Python Forum
while loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: while loop (/thread-16695.html)



while loop - sh98765 - Mar-10-2019

I'm very new to python
and would like to know what will be the output of the below code.

x=1
y=1
while y>=30
x= x+1
y=y+x
print (x)
I'm getting 1 as an output. I don't understand why. Thanks in advance!


RE: while loop - buran - Mar-10-2019

the output will be at least 2 errors:
SyntaxError for missing colon at the end of line 3 and IndentationError for missing body of the while loop


RE: while loop - sh98765 - Mar-10-2019

x=1
y=1
while y>=30:
  x= x+1
  y=y+x
print (x)
I'm getting 1 as an output. Is that correct because options for this coding are 7, 8, 9, 10 and none of these are the output I'm getting


RE: while loop - hshivaraj - Mar-10-2019

(Mar-10-2019, 10:19 PM)sh98765 Wrote: I'm getting 1 as an output. Is that correct because options for this coding are 7, 8, 9, 10 and none of these are the output I'm getting

1 is the correct answer. Because you while y >= 30 condition immediately failed and exits the loop and prints x; which at this point is 1. Perhaps you wanted this while y <= 20?


RE: while loop - sh98765 - Mar-10-2019

The code is provided by the teacher and I think it's incorrect. I just wanted to confirm.

Thank you


RE: while loop - hshivaraj - Mar-10-2019

More than certain its incorrect or perhaps you got the while loop exit condition wrong. You must cross check your code with your teachers code.