Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop
#1
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!
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
(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?
Reply
#5
The code is provided by the teacher and I think it's incorrect. I just wanted to confirm.

Thank you
Reply
#6
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020