0

While Loop Python Question

While Loop count down https://code.sololearn.com/ciW7tYm1qpUj/?ref=app

10th Apr 2020, 9:31 PM
Leonardo Monterey
Leonardo Monterey - avatar
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
10th Apr 2020, 9:38 PM
M Tamim
M Tamim - avatar
+ 3
# while loop x=y=3 z=x+y while z>2: print (z) z=z-1
10th Apr 2020, 9:36 PM
Cmurio
Cmurio - avatar
+ 2
You were just printing the variable x, you never saved the value of the sum of x and y in a variable
10th Apr 2020, 9:38 PM
Cmurio
Cmurio - avatar
+ 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
11th Apr 2020, 9:56 AM
rachel oyugi
+ 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.
12th Apr 2020, 6:52 AM
Pranay Nayak
+ 1
you print only the value of x..3 instead of x+y
10th Apr 2020, 9:40 PM
Justus
Justus - avatar
+ 1
Replace line number 7 with "print (x+y) " and here you go. :-)
12th Apr 2020, 4:44 AM
ClassHacker
ClassHacker - avatar
+ 1
That was a good one ClassHacker :p
12th Apr 2020, 5:01 AM
Leonardo Monterey
Leonardo Monterey - avatar
+ 1
You were printing the value of x not the sum.
12th Apr 2020, 4:47 PM
xpan
xpan - avatar
0
x=y=3 while (x+y)>2: print (x+y) x=x-1
10th Apr 2020, 9:39 PM
Justus
Justus - avatar
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?
10th Apr 2020, 9:44 PM
Leonardo Monterey
Leonardo Monterey - avatar
0
x by its self is 3, I can understand that
10th Apr 2020, 9:46 PM
Leonardo Monterey
Leonardo Monterey - avatar
0
wow , thanks all of you, that was mind boggling
10th Apr 2020, 9:50 PM
Leonardo Monterey
Leonardo Monterey - avatar