Jewenile Jewenile - 4 years ago 126
Python Question

WHILE result<value, argument**2

New to Python and programming, following an exercise from a book.

The program should take a value and keep printing "to the power of '+1' " on every new line, using WHILE.

My code:

x = 2

def powerof2_table_while(victim):
while result < 100:
""" want to write: 1.: 2 to the power of 1 is 2
2.: 2 to the power of 2 is 4
3.: 2 to the power of 3 is 8 """
print (line,".:\t", victim, "to the power of\t",line,"\t is", result)
return line
return line


Instead of returning the table of line + victim to the power of (line) it gives back only the first line and then stops.

May I humbly ask for a help? Thanks a lot!

Answer Source

As well as the unnecessary return, you also never update the result variable, you should move this inside of the while loop to avoid the infinite loop and incorrect outputs

  while result < 100:

should be

while result < 100:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download