8. Mini Project: Your Text Adventure, in Python¶
Rebuild the same text adventure from Unit 2 — same rooms, same decision, same loop — but now as a Python script, with a dictionary for rooms and a function for moving between them.
A skeleton to start from:
rooms = {
"cave": {
"description": "You are in a dark cave.",
"exits": {"left": "dragon_room", "right": "door_room"},
},
"dragon_room": {
"description": "A sleeping dragon blocks the way.",
"exits": {},
},
"door_room": {
"description": "A locked door stands before you.",
"exits": {},
},
}
current_room = "cave"
health = 3
while health > 0:
room = rooms[current_room]
print(room["description"])
if not room["exits"]:
print("The adventure ends here.")
break
choice = input(f"Where do you go? {list(room['exits'].keys())}: ")
if choice in room["exits"]:
current_room = room["exits"][choice]
else:
print("You can't go that way.")
This covers everything from the unit: a dictionary for rooms and exits, a while loop that keeps the game running, an if for valid/invalid choices, and a variable (health) that could end the game early.
Try it
Finish the script with all 3+ rooms from your original plan, at least one loop that can repeat (a fight, a search, a retry), and a way for health to actually change and end the game.
Tip
input(...) pauses the program and waits for the player to type something — whatever they type comes back as a str, even if it looks like a number.