Generating Perfect Squares python -
i trying write simplest code possible continuously print out perfect squares. code far reads this
x = 1 y = 1 while true: final = x * y print final x = x + 1 y = y + 1 when run it, syntax error. red bit on "final" in "print final"
apologies if basic error, i'm stumped.
thanks taking time read this
i assume you're using python 3. print function in python 3.
do print(final) instead of print final.
also, seems x , y values holding same thing. why not discard 1 of them , use one?
x = 1 while true: final = x * x print(final) x = x + 1 or better yet, use builtin exponentiation operator **.
x = 1 while true: final = x **2 print(final) x += 1 also, code seems going infinite loop. may need condition break it.
for example, if want break when x reaches 10, add condition in while loop, follows:
x = 1 while true: final = x **2 print(final) x += 1 if x == 10: break or specify condition in whilestatement, follows:
x = 1 while x < 10: final = x **2 print(final) x += 1 or use for loop.
for in range(10): print(i**2)
Comments
Post a Comment