Skip to content
BetaDE
Lesson 9 of 35Python

Python · Lesson 9 · 45 minutes

Project: text adventure

What you'll learn

  • Build a game loop with while
  • Store a game world as a nested dictionary
  • Check input and react clearly

Your first real game

Now you put together everything you’ve learned: variables, if, loops, dictionaries and input(). The result is a text adventure – a game where you move through a world by typing commands. Games like this existed before computers could even show graphics!

The world as a dictionary

Every room is a key. Its value is another dictionary: which direction leads to which room?

world = {
    "hall": {"north": "kitchen", "east": "lab"},
    "kitchen": {"south": "hall"},
    "lab": {"west": "hall"},
}

room = "hall"
print(world[room])
print(world[room]["east"])
print("north" in world["lab"])

Read world[room]["east"] from left to right: first the room, then the direction.

The game loop

Almost every game has a game loop: show the state → read input → change the state → repeat. It runs until the game is over:

energy = 3
while energy > 0:
    print(f"Energy: {energy}")
    command = input("What do you do? (run/rest) ")
    if command == "run":
        energy -= 1
    elif command == "rest":
        energy += 1
        print("You take a rest.")
    else:
        print("I don't know that command.")
print("You're exhausted. Game over!")

Cleaning up input

Players like to type East, east or EAST. With two string methods you always turn that into the same thing:

text = "  EaSt "
clean = text.strip().lower()
print(f"[{clean}]")

Keep building: When your task works, make the game bigger! Add rooms, put items in rooms ("item": "key") or lock the treasury until you have the key. Then share your game with the 🔗 button.

Your task

Your turn

Program a small text adventure. The game world is already in the dictionary world. You start in the hall. In every round print You are here: hall and ask for a direction with input("Where to? "). If the direction exists, go to the new room, otherwise print You can't go that way. When you reach the treasury, print You found the treasure! followed by the number of moves, e.g. Moves: 2, and end the game.

world = {
    "hall": {"north": "kitchen", "east": "lab"},
    "kitchen": {"south": "hall"},
    "lab": {"west": "hall", "north": "treasury"},
    "treasury": {},
}

room = "hall"
moves = 0

# Your game loop

Solve it without hints to earn a ⭐ on your progress page.

Show solution

Try it yourself first – mistakes teach you the most. Here is one possible solution:

world = {
    "hall": {"north": "kitchen", "east": "lab"},
    "kitchen": {"south": "hall"},
    "lab": {"west": "hall", "north": "treasury"},
    "treasury": {},
}

room = "hall"
moves = 0

while room != "treasury":
    print(f"You are here: {room}")
    direction = input("Where to? ").strip().lower()
    if direction in world[room]:
        room = world[room][direction]
        moves += 1
    else:
        print("You can't go that way.")

print("You found the treasure!")
print(f"Moves: {moves}")