🐸

Skipping Stones

Python Basicspython-basics-22-skipping-stones
Reward: 105 XP
|

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

1
River Crossing

Write a for loop that runs 5 times (range(5)).

2
Spot Moss

Inside the loop, check if i == 2.

3
Jump Over

If i is 2, use continue to skip it.

4
Land

After the if statement (but still inside the loop), print i.

Order Matters

If you print before the continue, you will print the number anyway! Make sure continue happens before the action you want to skip.

Suggested Solution
Click to expand

Safe crossing:

for i in range(5):
  if i == 2:
      continue
  print(i)
Advanced Tips
Want more? Click to expand

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.

pymain.py
Loading...
Terminal
Terminal
Ready to run...