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):
line=1
result=victim**(line)
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)
line=line+1
return line
return line

resultat=powerof2_table_while(x)
print(resultat)


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

result=victim**(line)
  while result < 100:

should be

result=0
while result < 100:
    result=victim**(line)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download