python · level 1 · unit 5 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
stops a loop early. skips a turn.
Picture a lift going up a tall building. You ride past each floor, and when you reach yours, you get out. You do not ride to the top. break is getting out: the loop stops straight away.
Some buildings have no floor 13, so the lift skips it and carries on. continue is that skip: Python drops the rest of this turn and jumps to the next one.
Both usually sit inside an if, so they only happen when its condition is True. Any lines after them in the same turn do not run.
for floor in range(10, 16):
if floor == 13:
continue
if floor == 15:
break
print("Ding! Floor", floor) Floor 13 is skipped by continue, and break ends the ride at floor 15.
guess first
What does continue do here?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
for song in range(1, 5):
if song == 2:
continue
print("Playing song", song)
your turn
Your turn. Choose what to skip and where to stop.
Type a number to skip in the first blank and a number to stop at in the second. Pick from 1 to 10, then press run.
fix it
Skip burnt cookie 3, but box all the others. Can you fix it?
- Run it. Cookies 4 and 5 never get boxed. Which word made the loop give up?
- break stops the whole loop. You only want to skip one turn.
- Swap break for continue, then run it again.
nailed it
you can stop a loop whenever you like. :D
breakstops the whole loop straight away.continueskips the rest of this turn only.- Put them inside an
ifso they happen at the right moment.
new words in your collection
- break
- continue
This lesson isn’t saved. A free account remembers every lesson you finish.