Question

At the moment I am taking an online course for python, only about 1/3 the way through and I decided to try and make something with what I've learnt so far. Running into an error now though. I am creating a text-based adventure sort of game in a house. Every room is a seperate function. EX:

def hallway():
hallway_direction = raw_input('blahblah')
if hallway_direction == 'n':
    living_room()

Although I have one room where you need a torch to enter. I used a dictionary for holding any values for rooms here is what I have.

global rooms

rooms = {}
rooms['first_room'] = {'note' : False}
rooms['old_door'] = {'boots' : False}
rooms['first_again'] = {'torch' : False}
rooms['first_again'] = {'seen' : False}

In another room it sets torch to true, but the problem I'm having is that if you don't have the torch I need it to take you back to the hall

def fancy_door():
    raw_input('You open the door, the inside is pitch black. You need a source of light before you can enter.')
    if rooms['first_again']['torch']:
        raw_input('You light the torch and step inside, the room is bare, only a table with a ring in the center.')
        choice5_r = raw_input('Do you take the ring? Y/N ("back" to leave)')
        choice5_r = choice5_r.lower()
        if choice5_r == 'y':
            raw_input('Some text here')
            darkness()
        elif choice5_r == 'n':
            raw_input('You leave the ring as it is.')
            fancy_door()
        elif choice5_r == 'back':
            hall()
        else:
            raw_input('Not a valid option')
            fancy_door()
    else:
        hall()

When I run this however I get this error:

Traceback (most recent call last):
File "<stdin>", line 247, in <module>
File "<stdin>", line 23, in first_room
File "<stdin>", line 57, in hall
File "<stdin>", line 136, in fancy_door
KeyError: 'torch'

On line 247 it calls first_room() which works up until this point. 23 calls hall() which works until this point. 57 calls the fancy_door() which should be working it looks as the same as the other door functions and they work fine. line 136 is the line above "if rooms['first_again']['torch']:"

If the problem isn't here I can post the entirety of the code on here or pastebin, I didn't only because it was 230 lines long.

If someone could help me out I'd be very greatful.

Also, please excuse the bad code, I know it probably doesn't follow proper conventions but like I said, I'm new to Python, and programming in general. This is the first thing I've ever written. Thanks in advance!

Was it helpful?

Solution

in your definition of the global variables, you define rooms['first_again'] twice.

Each time you assign a value to an element of a dict:

rooms['first_again'] = #something

you overwrite what was there previously.

It's saying

KeyError: 'torch'

because that object no longer has an element called torch.

Try changing it to:

rooms['first_again'] = {'torch' : False, 'seen' : False}

Or if you need to add values to that element later, you can do:

rooms['first_again'] = {'torch' : False}
rooms['first_again']['seen'] = False

OTHER TIPS

You've assigned the rooms['first_again'] twice.

rooms['first_again'] = {'torch' : False}
rooms['first_again'] = {'seen' : False}

Maybe it should be:

rooms['first_aggin'] = {}
rooms['first_again']['torch'] = False
rooms['first_again']['seen'] = False

The clue here is KeyError: 'torch'. This error occurs when you attempt to access a key within a dictionary which does not exist.

Look like the problem lies with how you deal with rooms['first_again']. You show the following code:

rooms['first_again'] = {'torch' : False}
rooms['first_again'] = {'seen' : False}

rooms is a dictionary which has several keys, one of which is 'first_again'. When you reference rooms['first_again'] you retrieve the object that corresponds to this key. What you actually did here is nest another dictionary.

The first assignment assigns a dictionary {'torch' : False} to rooms['first_again']. The second assignment does something very similar but it overwrites the first assignment. The object containing your value for torch no longer exists!

If you want to have multiple values available at the same key, put these values together in a single dictionary.

rooms['first_again'] = { 'torch' : False, 'seen' : False }

Now you can reference the values exactly as you attempted to in your original code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top