← Back to Loops Part 2
πŸšͺ

Find the Exit

2 of 6

Exercise 5: Find the Exit

You're exploring a dungeon with a series of rooms. Loop through the rooms and print what you find in each one. When you reach the room called "exit", print a victory message and stop exploring using break.

Use this list of rooms:

rooms = ["empty", "goblin", "empty", "treasure", "exit", "dragon", "empty"]

Expected output:

Room 1: empty
Room 2: goblin
Room 3: empty
Room 4: treasure
Room 5: exit - You found the exit!
Explored 5 rooms.

Notice that "dragon" and the last "empty" are never printed β€” the loop stopped at "exit".


Requirements

  • Use a for loop to go through the rooms
  • Use break to stop when you find "exit"
  • Track how many rooms you explored
  • No starter code β€” write the whole solution yourself!

Hints

Hint 1 β€” How to number the rooms

You can use enumerate() to get both the index and the room name:

for i, room in enumerate(rooms):

The room number for display is i + 1 since enumerate starts at 0.

Hint 2 β€” When to break

Check if the current room is "exit" using an if statement. If it is, print the special message and break.

Hint 3 β€” Tracking rooms explored

You can use a counter variable that increments each loop, or you can use i + 1 from enumerate after the loop ends (since i keeps its last value after a for loop).

πŸ’»

Try it yourself

Code: Find the Exit

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