← Back to Loops Part 2

Dodge the Obstacles

3 of 6

Exercise 6: Dodge the Obstacles

You're running through an obstacle course. The path has items you can collect and spikes you must dodge. Loop through the path and use continue to skip any "spike" items. Collect everything else and count how many items you picked up.

Use this path:

path = ["coin", "spike", "coin", "coin", "spike", "gem", "spike", "coin"]

Expected output:

Collected: coin
Skipped: spike
Collected: coin
Collected: coin
Skipped: spike
Collected: gem
Skipped: spike
Collected: coin
Total items collected: 5

Requirements

  • Use a for loop to go through the path
  • Use continue to skip "spike" items (but still print "Skipped" before continuing)
  • Keep a counter of how many items you collected
  • No starter code — write the whole solution yourself!

Hints

Hint 1 — Print before you continue

You need to print "Skipped: spike" before calling continue, because continue skips everything after it:

if item == "spike": print(f"Skipped: {item}") continue
Hint 2 — Counting collected items

Create a variable like collected = 0 before the loop. After the spike check (which uses continue), increment it by 1 for each non-spike item.

Hint 3 — Structure of the loop body

The order inside the loop should be:

  1. Check if spike → print skip message → continue
  2. Print collected message
  3. Increment counter
💻

Try it yourself

Code: Dodge the Obstacles

Loading Python runtime…
Python
Loading...
Output
Click "Run" to execute your code...
ℹ️ About this Python environment

✅ Standard library: heapq, collections, itertools, math, random, functools, datetime, bisect

✅ Functions, classes, recursion, print()

❌ No file system, subprocess, OS access, or network requests

❌ No pip install (all supported modules are pre-loaded)

⏱️ 5 second execution time limit