0

Why does the countdown start on 3 when the print says i = i - 1 if i = 3?

21st Jul 2016, 9:47 AM
Daniel
3 Respostas
0
Yes the countdown starts 3 because before the code i = i - 1 runs, "i" has already been set to 3. So after the code runs, then "i" will have new value of 2 and keep running down. But there is no condition to check when to stop, the syntax above isn't complete. It should be: i = 3 while True: if i > 0: print(i) i = i - 1 else: break
21st Jul 2016, 10:15 AM
Benneth Yankey
Benneth Yankey - avatar
0
I get that I just dont see the logic of printing i - 1 and the first number showing has not been substracted by - 1 when the code says print i, which in this case is 3, - 1 which would result in 2. As I was typing just now tho I realised that my view of how a loop works in python is wrong.
21st Jul 2016, 1:54 PM
Daniel
0
Yeah I guess, so print is done before subtraction, and after subtraction prints again before next subtraction until the condition is false and breaks out of loop. Glad I could help....happy coding!
21st Jul 2016, 2:02 PM
Benneth Yankey
Benneth Yankey - avatar