## boogiepicker31 2 years ago 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

• This Question is Open
1. Taufique

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)

2. boogiepicker31

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.