Python Forum

Full Version: while loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
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
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
(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?
The code is provided by the teacher and I think it's incorrect. I just wanted to confirm.

Thank you
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.