Here's the question you clicked on:
kah712
I am having a little trouble with this question: "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." This is what I came up with: x = (raw_input ("Enter a base:")) y = (raw_input ("Enter an exponent:")) exponential = pow(x, y) for solution in exponential: print "answer =", solution When I run it, an error comes up for line 3 with this message: TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'str'
you need to convert x and y into integer or float, because by default raw_input returns a string. You convert one variable to another type by using appropriate type conversion functions such as float() or int() See more here: http://docs.python.org/2/library/functions.html#int http://docs.python.org/2/library/functions.html#float
Thanks! I changed it from raw_input to input since I want it to be an integer..but now a new error is coming up, here is the code: x = input ("Enter a base:") y = input ("Enter an exponent:") a = pow(x, y) for solution in a: print solution and here is the error message: for solution in a: TypeError: 'int' object is not iterable
well, pow(x,y) returns a single number -- x to the power o y. Since it's single number you can't iterate over it. You can only iterate over lists. In your case you just need to print a itself. Now mostlikely what the task question was alluding towards is that you should NOT use internal pow() function, but rather implement it yourself.
So it works when I just print a itself, so a = x**y, but I was confused because the problem stated that it should be written as a for loop, would you be able to show me what that looks like?
Mostlikely they want you to not use ** or pow() but calculate exponent in a loop, looking through multiplication. So let's look at example -- lets say you need to compute 2 to the power of 4. You can write it out as: 2 ^ 4 = 2 * 2 * 2 * 2 See the looping here? So that's what you need to implement. I'll give you a hint and starting point: x = input ("Enter a base:") y = input ("Enter an exponent:") exponent = 1 for i in range(y): exponent = ???? so you will only need to fill in ??? :)
Im also still trying to solve this one without sucess...
@filiphdan What issues are you running into? (probably worth discussing in a separate question
c0decracker, thanks for being so thorough and helpful with your answers! I still can't figure this problem out though. First in your code why do you write exponent = 1 in the third line? I'm also not understanding how using the range function helps to translate the code into a loop. For example..if the goal is to calculate 2**4, and you use "for in in range(y)", the range of y would be (0,1,2,3) ...how do you get from there to a loop of 2 four times?
@kah712 Ok, so let's look at this code line by line, I am going to provide my explanation in the comments after each line: x = input ("Enter a base:") # as a result of this line x will contain a value for the base, in our example 2 y = input ("Enter an exponent:") # as a result of this line y will contain a value of the power, in our example 4 # which means that we need to multiply x by itself 4 times # so we can write this as : result = x^y = x*x*x*x exponent = 1 # to utilize the loop to calculate this we need a starting value for the result # because we are multiplying we need to have a starting value that will not # change the outcome of multiplication. What value to pick? 1 of course # because 1*anything = anything :) for i in range(y): # ok no we need to loop over multiplication 4 times. # there are 2 ways of doing it -- one is a general way(see below) and # second one is "python" way, or in parlance of python developers - pythonic way :) # range() function is pythonic way of doing it. # range basically generates a list of N values(where N is the parameter you pass to it) # so if we say range(4) it will give us (0,1,2,3) so 4 values. Then we use that list to just # loop over it's values. the values are not important, what's important is that # there are 4 of them which matches exactly how many times # we need to iterate over multiplication exponent = ????
So the other, more general and perhaps simpler way of writing that loop is this: x = input ("Enter a base:") y = input ("Enter an exponent:") exponent = 1 i = 0 while i < y: exponent = exponent * x i = i +1 Here we are using variable i as a counter.
ah okay! So I think I'm finally understanding how the range function is working. So in our example 2^4, range(y) creates a loop of : exponent = exponent * x Which is basically exponent = 1*2 (four times). Which gives us 2*2*2*2 Thank you so so much!!!
Almost exactly right :) One important detail. It is actually exponent = exponent * 2 four times. So let's look at the detail of how it works: In the first iteration of the look you will have this: exponent = 1 * 2 so after that exponent will be 2 On the second iteration of the loop you will have: exponent = 2 * 2 --> 4 On the third iteration: exponent = 4 * 2 --> 8 And on the forth iteration: exponent = 8 * 2 --> 16 Makes sense?
Yes it makes complete sense, so during the four loops, the value of "exponent" is actually changing each time it goes through a loop, taking the previous value? so if we were to do 4^3 the range would be (0,1,2) with exponent = 1, exponent = exponent * x exponent = 1*4 ---> exponent = 4 exponent = 4*4 ---> exponent = 16 exponent = 16*4 ---> exponent = 64 print exponent #this would print the last value of exponent, which would be 64!
thanks for the help c0decracker, i finaly got it, it was easier than i thought, i just needed to make the exponent = exponent*x after the for cycle and it was done :)