2 of 6
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".
for loop to go through the roomsbreak to stop when you find "exit"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.
Check if the current room is "exit" using an if statement. If it is, print the special message and break.
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).
β 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