Skipping Stones
Hoppy is crossing a river by hopping on stones. Most stones are safe, but the third one is covered in slippery moss!
The continue statement tells Python to stop the current loop iteration and jump straight to the next one.
The Skip Spell
Unlike break (which kills the loop), continue just skips the rest of the code for this turn.
for i in range(5):
if i == 3:
continue
print(i)
This prints 0, 1, 2, 4. It skips 3 because print is after continue.
Your Mission
Write a for loop that runs 5 times (range(5)).
Inside the loop, check if i == 2.
If i is 2, use continue to skip it.
After the if statement (but still inside the loop), print i.
If you print before the continue, you will print the number anyway! Make sure continue happens before the action you want to skip.
Suggested SolutionClick to expandClick to collapse
Safe crossing:
for i in range(5):
if i == 2:
continue
print(i)Advanced TipsWant more? Click to expandClick to collapse
Python also has a pass statement. It does... nothing! It's used as a placeholder when you need to write a line of code but don't want it to do anything yet.