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

Building a text based game in python

I am new to python and am doing a small text based game in python. The objective is to be able to move room to room. I keep receiving a key error as python isn’t recognizing one of the rooms. After I reach the living room I am then given a key error that says this:

if command in rooms[current_room].keys():
KeyError: 'Living room'
rooms = {
        'Entrance room': {'East': 'Master Bedroom'},
        'Master Bedroom': {'North': 'Family room'},
        'Family room': {'East': 'Dining Room', 'South': 'Master Bedroom'},
        'Dining Room': {'South': 'Living room', 'West': 'Family room'},
        'Living Room': {'South': 'Kitchen'},
        'Kitchen': {'South': 'Secret room', 'West': 'Master bedroom'},
        'Secret Room': {'West': 'Dungeon', 'North': 'Kitchen'},
        'Dungeon': {'East': 'Secret room'}
}
instructions = 'Welcome to the dragon slayer game, reach the dungeon to win. To move type: go North, go East, go West, go South'
directions = ['go North', 'go South', 'go East', 'go West']

print(instructions)
current_room = 'Entrance room'

while True:
    if current_room == 'Dungeon':
        print('Congratulations! You have reached the Dungeon and defeated the Dragon!')
        break
    
    print('You are in the {}.'.format(current_room))

    
    command = input('\nWhat do you want to do? ')  
    if command in directions:
        command = command.split()[1]
        if command in rooms[current_room].keys():
            current_room = rooms[current_room][command]
            print(current_room)
        else:
            
            print('You cant go that way!')
    
    elif command == 'exit':
        print('Thanks for playing!')
        break
    
    else:
        print('Invalid input')

>Solution :

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

You are checking for ‘Living room’ while there is ‘Living Room’ in the dictionary.
The r should be capital

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