So in this book chapter 3 subsection "Creating Intentional Infinite Loops" author gives this example:
# Finicky Counter
# Demonstrates the break and continue statements
count = 0
while True:
count += 1
# end loop if count greater than 10
if count > 10:
break
# skip 5
if count == 5:
continue
print(count)
input("\n\nPress the enter key to exit.")
But it doesn't work. It only spits out break-outside-loop and continue-not-properly-in-loop errors. From what I've read break/continue can't be used to break out of an if - it can only break out of loops, and that I should use sys.exit()
or return
. The question arises, what the author meant, and why he made this -basic?- mistake? Or maybe it isn't a mistake and I'm missing something.
Could you help me grasp this concept of break/continue function with fairly similar and simple example? :)
Indentations matter in python. So it must be,
count = 0
while True:
count += 1
# end loop if count greater than 10
if count > 10:
break
# skip 5
if count == 5:
continue
print(count)
input("\n\nPress the enter key to exit.")