Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
KeyError
#1
I used this tutorial as a template. Why does mine get a KeyError and the tutorial doesn't? I even named the variables the same just to make sure I was right! I find this happens a lot. I try to copy something exactly and my version doesn't work. I can't find the difference! Wall

rooms = {'numbers': {'o': 'one', 't': 'two'}}

current_room = rooms['numbers']

directions = ['o', 't']

while True:
    for i in directions:
        print(i)

    command = input('Choose one. ').strip()

    if command in directions:
        if command in current_room:
            current_room = rooms[current_room[command]]

        else:
            print("You can't do that.")

    elif command.lower() in ('q', 'quit'):
        raise SystemExit

    else:
        print("I don't understand that command.")
Output:
o t Choose one. o
Error:
Traceback (most recent call last): File "test.py", line 15, in <module> current_room = rooms[current_room[command]] KeyError: 'one'
Reply
#2
His dictionary is structured differently. I would prefer to type it out like this so you can see what is going on better
rooms = {
    'empty': {
        'name': 'an empty room', 
        'east': 'bedroom', 
        'north': 'temple',
        'text': 'The stone floors and walls and cold and damp.'},
    'temple': {
        'name': 'a small temple', 
        'east': 'torture', 
        'south': 'empty',
        'text': 'There are three rows of benches facing a small statue.'},
    'torture': {
        'name': 'a torture chamber', 
        'west': 'temple', 
        'south': 'bedroom',
        'text': 'There is a rack and an iron maiden against the wall\nand some chains and thumbscrews on the floor.'},
    'bedroom': {
        'name': 'a bedroom', 
        'north': 'torture', 
        'west': 'empty',
        'text': 'There is a large bed with black, silk sheets on it.'}
    }
The sub keys of name and text are just for show and info, but the ones that have directions are for commands. Your dictionary is missing this. And because you are using the exact for loop code, it is expecting to bounce back and forth. Notice that rooms['empty']['east'] value is 'bedroom' which is also the key to rooms.

Quote:
rooms = {'numbers': {'o': 'one', 't': 'two'}}
Your dictionary's values one and two is not a key for rooms creating a KeyError.

That is the main difference between your code and his.

If you structured it like this is would be similar, and not give a KeyError because it bounces back and forth like the original dictionary.
rooms = {'first': {
            'f': 'second'},
        'second': {
            's': 'first'}
        }
 
current_room = rooms['first']
 
directions = ['f', 's']
Recommended Tutorials:
Reply
#3
The dictionary rooms has not got a 'room' dictionary with the key 'one'.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020