Tuesday, 15 June 2010

Using a while loop to print out a power table in Python -


I am trying to create a program that will print a table of powers called to provide the user base , And then the maximum exponent. Then the program will print a table of powers based on this information. For example, if the user is typed in 2 for the base, and 4 for the maximum exponent, then the program will start with the first 2 ^ 1, then 2 ^ 2, then 2 ^ 3, and finally 2 ^ 4. I do not understand that what I have written is not working. I debug, and saw that my base value really grows by 1 and I do not know why.

>
main

ev = (input ("Enter the base number:")) max = eval (input ("Enter maximum exponent")) counter = 1 while counter & Lt; = Max: base = base ^ counter counter = counter + 1 print (base) main ()

base change while while loop you should keep base = base ^ counter , but you should keep it that you want. Your solution is:

  while the counter & lt; = Max: res = base ** counter counter + = 1 print (res)  

Note: Your program is currently printing with the input of 2 , 4 is 2 ^ 1 , 2 ^ 2 , 4 ^ 3 , 64 ^ 4


No comments:

Post a Comment