Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

KeyError in project

Doing a project for a class where I have to make a text based game, and I am getting a KeyError upon running my code.

These are the 2 parts of my code where it says the error is stemming from

Line 26

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def get_item(state):
    return rooms[state]['Item']

Line 42

item = get_item(state)

Here is my full code, I have been looking and just can’t seem to find the solution. If anyone can see what I am missing, I would really appreciate it.

rooms = {
    'Outside': {'South': 'Main Hall'},
    'Main Hall': {'North': 'Outside', 'East': 'Dining Room', 'West': 'Bedroom', 'South': 'Living Room'},
    'Bedroom': {'West': 'Room with Villain', 'South': 'Bathroom', 'Item': 'Hat'},
    'Dining Room': {'South': 'Kitchen', 'West': 'Main Hall', 'Item': 'Chair leg'},
    'Kitchen': {'West': 'Living Room', 'North': 'Dining Room', 'East': 'Garage', 'Item': 'Snack'},
    'Living Room': {'North': 'Main Hall', 'East': 'Kitchen', 'Item': 'TV Remote'},
    'Garage': {'West': 'Kitchen', 'Item': 'Bike'},
    'Bathroom': {'North': 'Bedroom', 'South': 'Room with Villain', 'Item': 'Toilet paper'},
    'Room with Villain': {'East': 'Bedroom', 'North': 'Room with Villain', 'Item': 'Witch'}

}
state = 'Outside'


def get_new_state(state, direction):
    new_state = state
    for i in rooms:
        if i == state:
            if direction in rooms[i]:
                new_state = rooms[i][direction]
    return new_state


def get_item(state):
    return rooms[state]['Item']


def show_instructions():
    print('Witch Hunt Adventure Game')
    print('Collect the 6 items to win the game, or the Witch will attack you.')
    print('Movement commands: go South, go North, go East, or go West')
    print("To add an item to your inventory: get 'item_name'")


show_instructions()
Inventory = []
items = ['Hat', 'Chair leg', 'Snack', 'Bike', 'Toilet paper', 'TV Remote']
while (1):
    print('You are in ', state)
    print('Inventory:', Inventory)
    item = get_item(state)
    print('You see a ', item)
    print('--------------------')
    if item == 'Witch':
        print('YOU HAVE BEEN CAUGHT! GAME OVER')
        exit(0)
    direction = input('Enter your move: ')
    if direction == 'go East' or direction == 'go West' or direction == 'go North' or direction == 'go South':
        direction = direction[3:]
        new_state = get_new_state(state, direction)
        if new_state == state:
            print('There is a wall there')
        else:
            state = new_state
    elif direction == str('get ' + item):
        if item in Inventory:
            print('Item already taken, go to another room!')
        else:
            Inventory.append(item)
    else:
        print('Invalid input or move or item')
    if len(Inventory) == 6:
        print('Congratulations! After collecting all of those items, you have defeated the witch!')
        exit(0)

>Solution :

Some of your rooms don’t have items in them, for example

rooms['Outside'] = {'South': 'Main Hall'}

so attempting to access rooms['Outside']['Item'] will fail. You should perhaps use:

def get_item(state):
    return rooms[state].get('Item', None)

This will return None if there are no items in the room. In your main code you can then check for that:

    item = get_item(state)
    if item is not None:
        print('You see a ', item)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading