KeyError in project - python

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
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)

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)

Related

Displaying possible moves and receiving unwanted print statement

I need to add a function that displays the current possible moves. I am at a loss on how to do this. I also have a problem with a print statement occurring when I don't want it to. If I enter the "get item" input, it prints the "You can't do that." statement. That statement should only be for invalid commands. I am very new to Python and don't really understand much of what I am doing. Please help.
# Dictionary for rooms and directions
rooms = {
'Living Room': {'West': 'Garage', 'East': 'Kitchen', 'North': 'Bedroom', 'South': 'Den', 'item': 'None'},
'Garage': {'East': 'Living Room', 'item': 'Candle'},
'Bedroom': {'East': 'Bathroom', 'South': 'Living Room', 'item': 'Lighter'},
'Bathroom': {'West': 'Bedroom', 'item': 'Mirror'},
'Kitchen': {'North': 'Pantry', 'West': 'Living Room', 'item': 'Bowl of water'},
'Pantry': {'South': 'Kitchen', 'item': 'Salt'},
'Den': {'East': 'Basement', 'North': 'Living Room', 'item': 'Spellbook'},
'Basement': {'West': 'Den', 'item': 'None', 'villain': 'Ghost'}
}
current_room = 'Living Room' # Starting room
# Game instructions
def instructions():
print('Collect all 6 items and defeat the ghost!')
print('To move rooms, type: "North, South, East, or West".')
print('To collect items, type: "get _item_".')
print('To exit game, type: "Exit".')
# Player status
def show_status(current_room, inventory):
print('----------------------')
print('Your are in the', current_room)
print('Inventory: ', inventory)
print('You found {}'.format(rooms[current_room]['item']))
print('----------------------')
def move_rooms(current_room, direction): # Loop for location
current_room = rooms[current_room]
new_room = current_room[direction]
return new_room
instructions()
direction = ['North', 'South', 'East', 'West']
inventory = []
# Game loop
while True:
show_status(current_room, inventory)
direction = input('\nWhat do you want to do?: ')
if current_room in ['Basement']: #Boss room loop
if len(inventory) == 6:
print('You cast a spell and freed the ghost! You win!')
else:
print('The ghost attacks you! You lose!')
break
item = rooms[current_room].get('item') # get the current item, or None if there isn't one
if item is not None and direction == 'get ' + item:
if item in inventory:
print('You already have this item in your inventory!')
else:
inventory.append(item)
if direction in rooms[current_room]: #valid move
current_room = move_rooms(current_room, direction)
if direction == 'Exit': # Exit game
print('Thanks for playing!')
break

My program has a key error when trying to add to a list in python

I have a project for a python class where for a project we have to make a game where we add an item into a list. We have to collect all of the items before finishing the game. I can move between rooms just fine but I keep getting a KEYERROR when I type Item to add to the list. What would be the cause of this?
def show_status():
print('You are in', location)
print(rooms[location].get('Item'), 'Available')
print('Possible moves:', *possible)
print('Friends:', inventory)
print('______________________________________')
def get_item(location, rooms, inventory):
# add item to inventory and remove it from the room
inventory.append(rooms[location][Item])
del rooms[location]['Item']
rooms = {
'Gorillas cage': {'North': 'Bear exhibit', 'South': 'Gator swamp', 'East': 'Monkeys cage', 'West': 'Lions den'},
'Gator swamp': {'North': 'Gorillas cage', 'East': 'Fox den', 'Item': 'Toothless'},
'Fox den': {'West': 'Gator swamp', 'Item': 'Gus'},
'Exit': {'South': 'Monkeys cage', 'Item': 'Zookeeper'}, # villain
'Monkeys cage': {'North': 'Exit', 'West': 'Gorillas cage', 'Item': 'Abu'},
'Lions den': {'East': 'Gorillas cage', 'Item': 'Simba'},
'Bear exhibit': {'South': 'Gorillas cage', 'East': 'Reptile house', 'Item': 'Icebox'},
'Reptile house': {'West': 'Bear exhibit', 'Item': 'Sammy'}
}
location = 'Bear exhibit'
inventory = []
direction = ''
instructions()
item = rooms[location].get('Item')
while True:
possible = rooms[location].keys() # needs to be in the while loop
show_status() # shows possible moves and available friends in the room
direction = input('Which direction would you like to move?').strip().capitalize()
if direction in rooms[location].keys():
if item is not None and direction == 'item':
get_item(location, rooms, inventory)
else:
location = rooms[location][direction]
I think you have error here:
if item is not None and direction == 'item':
Should be direction == "Item" (capitalized) or direction.lower() == 'item'
and also here:
inventory.append(rooms[location][Item])`
Item is undefined, probably you want to use "Item"

Room Exploration; Issues with collecting items from a dictionary

I'm creating a room exploration game with Python but am having issues with item collection and the win condition being met (collecting all items to 'win' and losing if not all items are collected). Room movement, help and quitting function but I'm confused where to begin with items. I created an elif for 'get' to collect items but it didn't work. If I could get help with getting items from my dictionary would help a lot. Thank you.
rooms = {'Start room': {'name': 'Start room', 'west': 'West room', 'east': 'East room', 'north': 'Hallway',
'text': 'You are in the Start room.'},
'Hallway': {'name': 'Hallway', 'north': 'Hallway2', 'item': 'chips',
'text': 'You are in the Hallway.'},
'Hallway2': {'name': 'Hallway2', 'north': 'Hallway3', 'item': 'soda',
'text': 'You are in the Hallway2.'},
'Hallway3': {'name': 'Hallway3', 'north': 'Final room', 'item': 'stick',
'text': 'You are in the Hallway #3.'},
'Side room': {'name': 'Side room', 'east': 'Hallway3', 'item': 'cookie bug',
'text': 'You are in the Side room.'},
'Final room': {'name': 'Final room', 'south': 'Hallway3',
'text': 'You are in the Final room.'}, # boss room
'West room': {'name': 'West room', 'east': 'Start room', 'item': 'crown',
'text': 'You are in the West room.'},
'East room': {'name': 'East room', 'west': 'Start room', 'item': 'cape',
'text': 'You are in East room.'}}
def get_item(item_wants, current_room, inventory):
if 'item' in rooms[current_room]:
item_can_get = rooms[current_room]['item']
if item_wants != item_can_get.lower():
print(f'There is no {item_can_get} in this room')
else:
print(f'You just picked up {item_wants}')
inventory.append(item_wants)
return item_wants
def help_file(DIRECTIONS):
print(f"Use {DIRECTIONS} to move! If you would like to quit, type 'quit'.")
def get_input():
arg = '' # default if no arg
input_list = input('Enter move, get or exit:').split()
command = input_list[0]
if len(input_list) > 1:
arg = input_list[1]
return command, arg
def main():
inventory = []
DIRECTIONS = ['north', 'south', 'east', 'west']
current_room = 'Start room'
while True:
# location
print('You are in {}.'.format(current_room))
print("Inventory:", inventory)
# get user input
c, direction = get_input()
# moving
if c == 'move' and direction in DIRECTIONS:
if direction in rooms[current_room]:
current_room = rooms[current_room][direction]
else:
print(f'You cannot go {direction} from this room')
elif c == 'get':
pass
elif c == 'help':
help_file(DIRECTIONS)
elif c == 'quit':
print('Game quitting...')
break
else:
print('Invalid statement')
if current_room == 'Final room' and len(inventory) < 6:
print('You lost ! You forgot to collect all the items! GAME OVER...')
break
if current_room == 'Final room' and len(inventory) >= 6:
print('You did it! You collected all the items and are with your cool new friend! The end!')
break
print()
main()
In testing out your code it appeared where you were getting stuck was with the "getting" of items within a room. With that, I focused on tweaking the "get_item" function. Following is a copy of your code with some tweaks to the dictionary so that one could move about rooms a little easier and a refined "get_item" function.
rooms = {'Start room': {'name': 'Start room', 'west': 'West room', 'east': 'East room', 'north': 'Hallway',
'text': 'You are in the Start room.'},
'Hallway': {'name': 'Hallway', 'north': 'Hallway2', 'south': 'Start room', 'item': 'chips',
'text': 'You are in the Hallway.'},
'Hallway2': {'name': 'Hallway2', 'north': 'Hallway3', 'south': 'Hallway', 'item': 'soda',
'text': 'You are in the Hallway2.'},
'Hallway3': {'name': 'Hallway3', 'north': 'Final room', 'south': 'Hallway2', 'west': 'Side room', 'item': 'stick',
'text': 'You are in the Hallway #3.'},
'Side room': {'name': 'Side room', 'east': 'Hallway3', 'item': 'cookie',
'text': 'You are in the Side room.'},
'Final room': {'name': 'Final room', 'south': 'Hallway3',
'text': 'You are in the Final room.'}, # boss room
'West room': {'name': 'West room', 'east': 'Start room', 'item': 'crown',
'text': 'You are in the West room.'},
'East room': {'name': 'East room', 'west': 'Start room', 'item': 'cape',
'text': 'You are in East room.'}}
item_list = ['chips', 'soda', 'stick', 'cookie', 'crown', 'cape'] # Added this for a simplified check of what items have been acquired
def get_item(item_wants, current_room, inventory): # Enhanced this function.
if 'item' in rooms[current_room]:
item_can_get = rooms[current_room]['item']
if item_wants != item_can_get.lower():
print(f'There is no {item_wants} in this room')
else:
got_item = False
for i in range(len(inventory)):
if (inventory[i] == item_wants):
print("you already have this item")
got_item = True
if (got_item == False):
print(f'You just picked up {item_wants}')
inventory.append(item_wants)
got_all = True
for i in range(len(item_list)):
if (item_list[i] not in inventory):
got_all = False
print("You need to find", item_list[i])
return item_wants
def help_file(DIRECTIONS):
print(f"Use {DIRECTIONS} to move! If you would like to quit, type 'quit'.")
def get_input():
arg = '' # default if no arg
input_list = input('Enter move, get or quit:').split()
command = input_list[0]
if len(input_list) > 1:
arg = input_list[1]
return command, arg
def main():
inventory = []
DIRECTIONS = ['north', 'south', 'east', 'west']
current_room = 'Start room'
while True:
# location
print('You are in {}.'.format(current_room))
print("Inventory:", inventory)
# get user input
c, direction = get_input()
# moving
if c == 'move' and direction in DIRECTIONS:
if direction in rooms[current_room]:
current_room = rooms[current_room][direction]
else:
print(f'You cannot go {direction} from this room')
elif c == 'get':
get_item(direction, current_room, inventory)
elif c == 'help':
help_file(DIRECTIONS)
elif c == 'quit':
print('Game quitting...')
break
else:
print('Invalid statement')
if current_room == 'Final room' and len(inventory) < 6:
print('You lost ! You forgot to collect all the items! GAME OVER...')
break
if current_room == 'Final room' and len(inventory) >= 6:
print('You did it! You collected all the items and are with your cool new friend! The end!')
break
print()
main()
Give that a try.

Need help fixing my python code for text adventure game

I'm having trouble trying to get my code to run properly. When I run the code it works fine. Moving between rooms isn't an issue and when it comes to collecting the items there's no problem. The problem comes when I get to where the boss is and I enter the command in order to complete the game. However, it doesn't seem to work correctly. I've tried to rearrange it, but when I did, the problem then became that I couldn't collect the items but could then execute the command to fight the boss. Here is the code down below:
# data setup
rooms = {
'Entry': {'name': 'Entry', 'go south': 'Great Hall'},
'Great Hall': {'name': 'Great Hall', 'go north': 'Entry', 'go south': 'Bedroom',
'go east': 'Dining Room', 'go west': 'Cellar', 'item': 'helmet'},
'Bedroom': {'name': 'Bedroom', 'go north': 'Great Hall', 'item': 'shield'},
'Dining Room': {'name': 'Dining Room', 'go north': 'Kitchen', 'go south': 'Library',
'go west': 'Great Hall', 'item': 'boots'},
'Kitchen': {'name': 'Kitchen', 'go south': 'Dining Room', 'item': 'wrist guard'},
'Library': {'name': 'Library', 'go north': 'Dining Room', 'item': 'sword'},
'Cellar': {'name': 'Cellar', 'go south': 'Dungeon', 'go east': 'Great Hall', 'item': 'breast plate'},
'Dungeon': {'name': 'Dungeon', 'boss': 'sorcerer'}
}
directions = ['go north', 'go south', 'go east', 'go west']
current_room = rooms['Entry']
player_inv = []
# game instructions
def game_instruction():
print('Welcome to the Text Adventure Game')
print('Collect all 6 items before reaching the Dungeon in order to defeat the Sorcerer')
print('Movement: go north, go south, go east, go west')
print('Collecting Items: collect + "item name"')
print('Fighting Boss: fight + "boss name"')
# print game instruction
game_instruction()
# game loop
while True:
# display current location
print('You are in the {}'.format(current_room['name']))
# display current inventory
print('Inventory: {}'.format(player_inv))
# get user input
command = input('What do you want to do? ')
# movement
if command in directions:
if command in current_room:
current_room = rooms[current_room[command]]
# display current room item
if 'item' in current_room:
print('You see a', current_room['item'] + '.')
# display boss in Dungeon
if 'boss' in current_room:
print('You see the', current_room['boss'] + '.')
# bad movement
else:
print("You can't go in that direction.")
# quit game
elif command == 'quit':
print('Thanks for playing!')
break
# collect items
elif command == ('collect ' + current_room['item']):
if 'item' in current_room:
print(current_room['item'], 'collected.')
player_inv.append(current_room['item'])
del current_room['item']
else:
print('There are no items in this room.')
# game win/lose conditions
elif command == ('fight ' + current_room['boss']):
if 'boss' in current_room:
if len(player_inv) == 5:
print('CONGRATULATIONS!\nYou have successfully defeated the Sorcerer.')
break
else:
print('GAME OVER!\nYou failed to collect all the items need to defeat the Sorcerer.')
break
# bad command
else:
print('Invalid Command')
It gives me the error message:
Traceback (most recent call last):
File "C:\Users\-----\Documents\TextBasedGame\TextBasedGame.py", line 59, in <module>
elif command == ('collect ' + current_room['item']):
KeyError: 'item'
Any ideas on how I can fix this?

Global scope not changing

I have a text-based game using python. My issue is that I can't move between rooms due to a variable not getting changed like it needs to.
I've used global and that results in another error. The code is as follows:
#The dictionary links a room to other rooms and items.
rooms = {
'Holding Cell': {'South': 'Training room','North': 'Cupboard', 'West': 'Study' },
'Cupboard': {'South': 'Holding Cell', 'Item': 'Health Potion'},
'Study': {'West': 'Armory', 'East': 'Holding Cell', 'Item': 'Mind key'},
'Armory': {'East': 'Study', 'Item': 'Sword and shield'},
'Training room': {'North': 'Holding Cell', 'East': 'Storage room', 'Item': 'Body key'},
'Storage room': {'West': 'Training room', 'Item': 'Armor set'},
'Prayer room': {'North': 'Dungeon Exit', 'Item': 'Soul key'},
'Dungeon Exit': {}
}
starting_room = 'Holding Cell'
current_room = starting_room
inventory = []
inventory1 = ['Health Potion', 'Sword and shield', 'Mind key', 'Soul key', 'Armor set', 'Body key']
inventory1.sort()
health = 100
def status():
inventory.sort()
print('-----------------------------')
print("Inventory: ", inventory)
print("Health: ", str(health))
print("Current room: ", current_room)
def main():
rooms = {
'Holding Cell': {'South': 'Training room', 'North': 'Cupboard', 'West': 'Study'},
'Cupboard': {'South': 'Holding Cell', 'Item': 'Health Potion'},
'Study': {'West': 'Armory', 'East': 'Holding Cell', 'Item': 'Mind key'},
'Armory': {'East': 'Study', 'Item': 'Sword and shield'},
'Training room': {'North': 'Holding Cell', 'East': 'Storage room', 'Item': 'Body key'},
'Storage room': {'West': 'Training room', 'Item': 'Armor set'},
'Prayer room': {'North': 'Dungeon Exit', 'Item': 'Soul key'},
'Dungeon Exit': {}
}
status()
current_room = starting_room
direction = input("Enter 'North/South/East/West' to move or 'Exit': ")
# user to exit
if direction == 'Exit':
print("Thanks for playing!")
exit(0)
# a valid move
elif direction in rooms[current_room]:
current_room = rooms[current_room][direction]
# invalid move
else:
print("Invalid Move. There's no room to the {}".format(direction))
def show_instructions():
print("Type 'North', 'South', 'East', 'North' to go in a direction. Type 'Exit' to leave the game!")
print("To get an item type 'Get (item).")
#provides instructions to player
show_instructions()
while 1:
main()
First problem: Use global correctly within the main function.
def main():
status()
global current_room # <--- here
current_room = starting_room # this is already defined outside the function, so really isn't needed
direction = input("Enter 'North/South/East/West' to move or 'Exit': ")
if direction == 'Exit':
print("Thanks for playing!")
exit(0)
elif direction in rooms[current_room]:
current_room = rooms[current_room][direction]
else:
print("Invalid Move. There's no room to the {}".format(direction))
Second problem: you are constantly resetting the current_room = starting_room each time through the main function, and not maintaining your state correctly.
The fix for that seems to be removing that line and just prompting for the direction input, and letting the flow pass to the elif block.

Categories