Python for Humans · Draft v0.2 · Oct 28, 2026 · Project · Use Download .py to keep your edited adventure.

Text Adventure: Explore the Castle

Optional project · After The Word-Prediction Generator

You should already know
The castle adventure; lists, dictionaries, functions, and dictionary values that are lists through The Word-Prediction Generator.
You'll build
A small world to explore, with a torch to collect and a crown to find.
You'll be able to
Separate the world description from the changing room and inventory.
You need
This page. Each example runs in its own code box; nothing to install.

Remember the castle's red and blue doors? Each choice led to an ending. This time, let's stay in the castle: walk between rooms, pick something up, and use it somewhere else.

This is an optional creative project, about 20–30 minutes. Start with the shared castle, then make it yours. The other course projects don't depend on it.

Begin with the Castle We Know

Here's the replaying adventure from Loops in a shorter form. Run it and try both doors. It includes everything it needs:

print("Welcome to the Mysterious Castle!")
again = 'yes'

while again == 'yes':
    door = input("Choose a door: red or blue: ")
    if door == 'red':
        print("You discover a sleeping dragon!")
    elif door == 'blue':
        print("You find a chest full of treasure!")
    else:
        print("You stay in the hallway.")
    again = input("Play again? (yes/no) ")

print("Thanks for visiting!")

Each trip forgets where we were. To explore a connected world, we need a current room that changes after a move, and an inventory that stays with us.

Make a Small Map

A room name can be a dictionary key. Its value can describe that room. A second dictionary can connect each room with a list of destinations, just as the generator connected words with lists of followers.

descriptions = {
    'hall': 'Two passages lead away from the castle hall.',
    'dragon': 'A sleeping dragon guards a torch. Borrow it quietly!',
    'treasury': 'Gold glitters in the torchlight. A crown waits here.'
}
exits = {
    'hall': ['dragon', 'treasury'],
    'dragon': ['hall'],
    'treasury': ['hall']
}
items = {'dragon': 'torch', 'treasury': 'crown'}

print(descriptions['hall'])
print(exits['hall'])

We're using names such as 'hall' as short room identifiers. The descriptions are free to be longer and sillier.

The items dictionary says where the torch and crown can be found. Leave its entries in place when picking something up; inventory membership will tell us whether we already have it.

Try it: Change a description. Then add a new room and its exits. Every destination should have both a description and an exits entry, so a player can arrive there without a missing-key error.

Remember Where You Are

Start room at 'hall' and inventory at [], before a while True: loop.

Inside the loop:

  1. Display the current room description and its destinations.
  2. Ask for a command. If it's a destination in exits[room], assign it to room.
  3. Add quit so the player can leave.

Put the display instructions in show_room(room, descriptions, exits). Its three arguments supply the room name and the two dictionaries; each call can describe the room we're in now.

Get movement working before collecting anything. You should be able to go dragon, hall, then quit.

Pack Something Useful

Add a take command. If the room is a key in items, look up its item. Append it to inventory only if it isn't already there. You can express that with the familiar if item in inventory: followed by else:.

The treasury is dark, so a move there succeeds only when the bag contains 'torch'. Picking up the crown is our goal and ends this little adventure.

Explore the Finished World

Here's a complete version to compare after you've tried those steps. Its maps, functions, and state are all included:

def show_room(room, descriptions, exits):
    print(descriptions[room])
    print('You can go to:')
    for destination in exits[room]:
        print(destination)


descriptions = {
    'hall': 'Two passages lead away from the castle hall.',
    'dragon': 'A sleeping dragon guards a torch. Borrow it quietly!',
    'treasury': 'Gold glitters in the torchlight. A crown waits here.'
}
exits = {
    'hall': ['dragon', 'treasury'],
    'dragon': ['hall'],
    'treasury': ['hall']
}
items = {'dragon': 'torch', 'treasury': 'crown'}

room = 'hall'
inventory = []
print('Welcome back to the Mysterious Castle!')

while True:
    show_room(room, descriptions, exits)
    print('Your bag:', inventory)
    command = input('Where next? (room name, take, quit) ').lower()
    if command == 'quit':
        break
    elif command == 'take':
        if room in items:
            item = items[room]
            if item in inventory:
                print('You already have it.')
            else:
                inventory.append(item)
                print('You take the', item)
                if item == 'crown':
                    print('You found the crown. You win!')
                    break
        else:
            print('There is nothing to take here.')
    elif command in exits[room]:
        if command == 'treasury':
            if 'torch' in inventory:
                room = command
            else:
                print('Too dark! Find a torch first.')
        else:
            room = command
    else:
        print("You can't do that here.")

print('Thanks for exploring!')

The .lower() on the answer lets DRAGON and dragon name the same destination. We still require the exact room identifier; dragon room isn't one of our keys.

Try these paths on fresh runs:

The map stays the same while room and inventory change. Those two changing values are the state we'll eventually need to save.

Make the Castle Yours

Choose one bonus:

Click Download .py below your working adventure to keep a copy of your code. This saves your program; it doesn't save the current room or inventory. After Files and the filesystem, Save Your Adventure will let the program save a player's progress, too.

Name one value that describes the world and one value that changes as the player explores it.