+ 1

Idk why this error is happening?

My code is like this: >>> i=0 >>> while 1==1: print(i) i=i+1 if i>=5: print("breaking") break Then I press enter then left arrow key and write the remaining last line: print("Finished") Then an error occurs saying : "invalid syntax". I thought it'd happening because I pressed left arrow key before writing the last line, so I wrote it again like this: >>> i=0 >>> while 1==1: print(i) i=i+1 if i>=5: print("breaking") break print("Finished") Then the output came like: 0 finish 1 finish 2 finish 3 finish 4 Breaking But the desired output be like: 0 1 2 3 4 Breaking Finished Please help me understand this.

14th Jul 2019, 12:56 PM
Vinayak Shivankar
Vinayak Shivankar - avatar
3 odpowiedzi
+ 3
It's because print('Finished') is in while loop, which will be occur over and over again. You have to put it outside of while loop
14th Jul 2019, 2:05 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
Also a tip for you. You can use i<=5 in while expression instead of using 1==1. It will simplify your code and become more readable.
14th Jul 2019, 2:08 PM
你知道規則,我也是
你知道規則,我也是 - avatar
0
But when I wrote it outside the loop it said 'invalid syntax'
14th Jul 2019, 2:21 PM
Vinayak Shivankar
Vinayak Shivankar - avatar