+ 1
In the message for the following code I did'nt understand why '0' came in the output. Can anyone help me
n = 3 while n > 0: print(n + 1) n -= 1 else: print(n) output: 4 3 2 0
3 odpowiedzi
+ 1
Hi, Harsha Koganti !
First you print out n + 1 three times in your while loop:
Loop 1: n = 3
-> print(n+1) = 3+1 = 4
Then you set: n-= 1 -> n = 2
Loop 2: n = 2
-> print(n+1) = 2+1 = 3
Then you set: n-= 1 -> n = 1
Loop 3: n = 1
-> print(n+1) = 1+1 = 2
Then you set: n-= 1 -> n = 0
Now n = 0, and in your else statement you print it out:
else: n = 0
-> print(n)
0
0
Thank you very much Miss Somya and Mrs Per Bratthammar