anonymous
  • anonymous
Anybody care to check this code and make sure it's correct? It works in any situation I've tried, looking for holes.... Question from OCW 6.189, Homework 1.8 #3: Write a program using a for loop that calculates exponentials. Your program should ask the user for a base 'base' and an exponent 'exp', and calculate base^exp. #if y is negative (< 0) then it is 1/x*x #if y is positive (> 0) then it is x*x #if y = 0 then answer is 1 x = input("Choose your base: ") y = input("Choose your expo: ") if y>0: print x**y elif y<0: print 1/x**y else: print 1
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
in your question,calculate exponential using for loop..so try following steps to find exponential.. for num in range(1,exp): # loop will move from 1 to exponet value. value=value*base #multiply base exp times print 'the exponential value is* %d' % (value)
anonymous
  • anonymous
Ok, here's the revised code. I've tested it a bunch of times, and it all works: x = input("Choose your base: ") y = input("Choose your expo: ") value = 1 for num in range(y): value = value*x print value However I still need some help. First of all, is there a more succinct/efficient way of writing this code? I'm sure there is. Secondly, I'm confused as to why my old code wasn't working. My old code looked like this: x = input("Choose your base: ") y = input("Choose your expo: ") for x in range(y): x = x*x print x And I would get answers like this: base = 3 expo = 3 0 1 4 I'm just wondering why it would give me those answers. Thanks for the help in advance, you guys are awesome.

Looking for something else?

Not the answer you are looking for? Search for more explanations.