Free lab Real Python 3. Zero installs. Your code stays in this browser. Open the playground

Capstone: Tiny Text Adventure

15 min 40 XP
Study loop Read Predict Run Tweak Prove Review
Lesson 5 of 5 · View course roadmap
Step 1

Learn the idea

Time to ship a game. Every text adventure — from 1977's Zork to modern interactive fiction — is built on one idea: the world is a dictionary.

  • Keys are room names: "hall", "library", "vault"
  • Values are what the player sees there
  • The player's journey is just a list of keys to visit

Walk the path with a loop, look each room up with rooms[name], and narrate. That's the whole engine. Want more rooms, items, or monsters? Add keys. The data grows; the code stays the same — that separation of data from logic is one of the biggest ideas in software, and you're about to use it.

Where you'll use this

Data-driven design — world as data, engine as code — is how real games ship: level editors write data files, the engine stays unchanged. It's also how CMSs, chatbots and workflow tools scale content without new code.

Common mistakes

  • A typo between a path entry and a room key → KeyError. Keys must match exactly, including case.
  • Putting story text in the loop instead of the dict — then every new room needs new code, defeating the design.
  • Printing the final message inside the loop (indented) so it repeats after every room.

Pro tip

Give each room a dict of exits — "hall": {"desc": ..., "exits": {"north": "library"}} — and your walk can become a real free-roaming game with about ten more lines.

Step 2

Try it yourself

Blank · autosaved

The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.

PYexample.py
+ Enter to run
Output appears here…
Step 3

Pass the challenge +40 XP

Blank · autosaved

Using the rooms and path in the starter, print You enter the {room}. followed by the room's description for each stop on the path — then print Quest complete! at the end.

Target output
You enter the hall.
A dusty hall. Doors lead north and east.
You enter the library.
Shelves of ancient Python books.
You enter the vault.
The vault! Treasure: 100 XP.
Quest complete!
PYchallenge.py
Run your code to check it…
Step 4

Check your understanding

1. In this game, what does rooms["vault"] do?
2. Why is separating the world (data) from the walk (logic) powerful?
Last step

Your notes (saved on this device)

Tip: use and to move between lessons, K to search everything.

Your next ten minutes

Write Python that does something useful.

Start free. No install, no card, no passive video marathon.

Start learning free → Explore the path