Python’s While and Continue: why does i += 1 placement matter? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python’s While and Continue: why does i += 1 placement matter?

For the Break & Continue plane ticket problem, I don’t understand why the placement of i+=1 matters. This code works: total = 0 i=1 while i<=5: i+=1 age = int(input()) if age > 3: total += 100 else: continue print(total) But if I move the i += 1 to the bottom of the “while” section, it says the age=int(input()) line results in an EOF error. total=0 i=1 while i<=5: age = int(input()) if age > 3: total += 100 else: continue i+=1 print(total)

21st Dec 2021, 5:47 PM
Mikayla
Mikayla - avatar
3 Answers
+ 1
As I know "continue" keyword ends the current loop iteration and jumps to the next one, so; in the second code, if the age is <=3 then "i+=1" won't execute, so it will require more inputs
21st Dec 2021, 6:48 PM
Sousou
Sousou - avatar
+ 1
Thank you!
21st Dec 2021, 7:05 PM
Mikayla
Mikayla - avatar
0
Mikayla welcome :)
21st Dec 2021, 7:12 PM
Sousou
Sousou - avatar