while loop - Square number sequence in Python -
i'm new python , trying make code print square numbers until square of desired value entered user.
n = raw_input("enter number") a=1 while < n: a=1 print a*a += 1 if > n: break when run code infinitely prints "1" ... i'm guessing value of a not increase += it's a=1 forever. how fix this?
there problems. first, input (what raw_input() returns) string, must convert integer:
n = int(raw_input(...)) second, setting a = 1 each iteration, so, since loop condition a < n, loop run forever ( if n > 1). should delete line
a = 1 finally, it's not necesary check if a > n, because loop condition handle it:
while < n: print * += 1 # 'if' not necessary
Comments
Post a Comment