Choose-your-path books
"Open the door? Go to page 12. Run away? Go to page 30."
12GAME PROJECTS · ADVENTURE
Build a text adventure: rooms, choices, a bag of things, and a hidden treasure.
IN REAL LIFE
You pick the path, and the story changes.
"Open the door? Go to page 12. Run away? Go to page 30."
You walk room to room, pick up keys, and open locked doors.
At a party, each clue sends you to a new place until you find the prize.
THE PLAN
Each step uses something you already learned.
A box keeps the room you are in. if and elif pick what happens there.
room = "hall" if elif
input() asks left, right or quit. An if inside another if makes a nested choice.
input() if
A list keeps what you find. append adds, and in checks.
[] append in
A while loop repeats until you find the treasure or quit.
while
RUN IT
Guess first. Then run real Python and check.
Read the game in the editor. Then look at PyBot's answers.
PyBot's answers: right, left, right
What is the last line Python shows?
PyBot's hint
Ready. Press Run Python.
Pyodide 314.0.3 runs CPython in your browser with WebAssembly.
Open-source under the Mozilla Public License 2.0. License
Now change one thing: Change PyBot's answers to: right, quit. Does the treasure line appear?
BUILD IT
Each task builds one part of an adventure. Match the goal.
In the tunnel, PyBot asks about the torch but does nothing with it. Add the if and else.
Door or tunnel? tunnel Light the torch? yes or no? yes You see gold!OUTPUT
PyBot finds a torch but forgets to keep it. Put it in the bag.
You find a torch! The cave lights up. Bag: ['torch']OUTPUT
The loop never lets the player quit. Add the missing elif.
Dig, walk or quit? walk You walk deeper into the cave. Dig, walk or quit? quit See you next time!OUTPUT
YOUR TURN
Try each tiny question. A wrong answer becomes something to review.
room = "hall"What does the box room keep in the game?
inventory = ["map"]
inventory.append("key")
print(inventory)What will Python show?
inventory = []
if "key" in inventory:
print("The key opens the chest.")
else:
print("The chest is locked.")What will Python show?
In the hall, the player types: LEFT
if choice == "left":
room = "library"Does the player go to the library?
elif room == "kitchen":
if "key" in inventory:When does Python check the if about the key?
while room != "treasure" and room != "quit":When does this loop stop?
Run the big game with these answers.
PyBot's answers: left, right
How many times does the game ask "left, right or quit?"
Want a fresh start? Clear only this page.
A box remembers where you are, a list remembers what you carry, and while keeps the story going.