Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (deltanov):

I am trying to make the exercise in python for week 2: 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 baseexp. Any ideas? I cannot come up with the algorithm. I tried: x=input("Give a base number:") y=input("Give an exp number:") if y>0: for x in range(y): x=x*x y=y-1 else: for x in range(y): x=1/x*x print x But of course it is not working:)

OpenStudy (anonymous):

base = 1 for i in range(y): # do it y times i takes the values 0,1,2,...(y-1), ....base = base * x # base = x then x*x then (x*x)*x then ...

OpenStudy (rsmith6559):

A couple of things. First, don't use input() in Python 2.x. In Python 3.x it does what you want, but in Python 2 you use raw_input( "prompt string" ) Second, in your for loop when y > 0, you're iterating on y, and then changing y's value. Definite no-no. Test it in the interpreter printing out y's values, you'll see that it's a disaster.

OpenStudy (deltanov):

Thanks a lot to both of you!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!