0
While Loop Python Question
While Loop count down https://code.sololearn.com/ciW7tYm1qpUj/?ref=app
13 Answers
+ 5
x=y=3
while x+y>2:
print (x)
x=x-1
look, here x = 3
not 6
but x + y = 6 (before going to loop)
On the time of first iteraion you print x (3) and reduce x's value to 2
then x + y = 5
and that's how the iteration goes on and prints
3
2
1
0
+ 3
# while loop
x=y=3
z=x+y
while z>2:
print (z)
z=z-1
+ 2
You were just printing the variable x, you never saved the value of the sum of x and y in a variable
+ 2
I think inorder for it to output 654321 the code should be
x=y=6
While x+y>6
Print(x)
X=x-1
+ 2
Don't know about the quiz but,
1st iteration: Your while loop began with the condition 6>3 ; which is true
The loop runs and x is printed and then reduced to 2 from 3.
2nd iteration: now check for 2+3>3, which is true.
Loop runs again and prints x (which is 2 now) and then reduces x from 2 to 1.
3rd iteration: x is 1 and y is still 3, so x+y>3, True again.
Loop runs and x, which is now 1, is printed then gets reduced to 0.
4th iteration: x is 0 and y is 3, x+y > 3 is FALSE now. (3>3 is Wrong).
LOOP DOESN'T RUN ANYMORE!
once the condition is false, loop is bypassed! Its work is done and you can forget about it now.
+ 1
you print only the value of x..3 instead of x+y
+ 1
Replace line number 7 with "print (x+y) " and here you go. :-)
+ 1
That was a good one ClassHacker :p
+ 1
You were printing the value of x not the sum.
0
x=y=3
while (x+y)>2:
print (x+y)
x=x-1
0
I still dont understnd how does it become a 5? and I can see that it is 3 then -1 it can go to zero?
0
x by its self is 3, I can understand that
0
wow , thanks all of you, that was mind boggling