0

Python while loops

x = int(input("Type in a number")) while x > 0: y = x while y > 0: y -= 1 print(y) x -= 1 print("stop") After I input 3, the output is: 2 1 0 1 0 0 stop I understand where 2, 1, 0 come from, but what's happening afterwards is unclear to me. Could you explain, please?

3rd Jul 2019, 12:32 PM
John Galt
John Galt - avatar
3 odpowiedzi
+ 2
It's because the while y > 0 is executed three time. After the first one (2, 1, 0) x = 2 x > 0 true y = 2 y > 0 true y -= 1 -> y = 1 print(y) -> 1 y -= 1 -> y = 0 print(y) -> 0 After that x = 1 x > 0 true y = 1 y > 0 true y -= 1 -> y = 0 print(y) -> 0 x -= 1 -> x = 0 x > 0 false print("stop")
3rd Jul 2019, 12:43 PM
Agent_I
Agent_I - avatar
0
Thanks, bruh. Have a nice day.
3rd Jul 2019, 12:52 PM
John Galt
John Galt - avatar
0
It's really all clear now. Great thanks.
3rd Jul 2019, 12:56 PM
John Galt
John Galt - avatar