Reputation: 11
I keep getting an unwanted result when I try to debug my program. It doesn't seem to recognize the code moves S, N, E, or W and prints the invalid message. I've been trying to work through this for hours and haven't gotten very far. I think it might have to do with the move len()? My other thought was the variables...maybe I mislabeled them?
def show_instructions(): # print mission title and instructions
print('Welcome to the Ultimate Museum Heist!')
print('If you wish to complete this mission, you must collect all the artifacts. Do not run into police!')
print('Move commands: S, N, E, W')
print('To steal artifacts type: get "item name"')
def move_between_rooms(location, move, exhibits):
# move to corresponding exhibits
location = exhibits[location][move]
return location
def get_item(location, move, exhibits, inventory):
# add item to inventory and remove it from the exhibit
inventory.append(exhibits[location]['item'])
del exhibits[location]['item']
def main(): # define an inventory of artifacts, letting the player know they start with none
exhibits = {
'Lobby': {'S': 'South Exhibit', 'N': 'North Exhibit', 'E': 'East Wing Exhibit', 'W': 'West Wing Exhibit'},
'South Exhibit': {'E': 'Basement', 'N': 'Lobby', 'item': '1849 Double Eagle $20 Coin'},
'Basement': {'W': 'South Exhibit', 'item': 'Hope Diamond Necklace'},
'East Wing Exhibit': {'W': 'Lobby', 'N': 'Security Headquarters', 'item': 'Dead Sea Scrolls'},
'West Wing Exhibit': {'E': 'Lobby', 'item': 'Diamond Panther Bracelet'},
'North Exhibit': {'E': 'North West Exhibit', 'S': 'Lobby', 'item': 'Pinner Qing Dynasty Vase'},
'North West Exhibit': {'W': 'North Exhibit', 'item': 'Gold-Encrusted Sword'},
'Security Headquarters': ''
}
list = ' '
# list for storing agent inventory
inventory = []
# starting room
location = 'Lobby'
# show the player the instructions
show_instructions()
while True:
# handle the case when player encounters the police
if location == 'Security Headquarters':
# winning case
if len(inventory) == 6:
print('BRIBE POLICE TO ESCAPE:\n')
print('MISSION SUCCESSFUL!')
break
# losing case
else:
print('\nMISSION FAILED.\n')
print('CAPTURED BY POLICE')
break
# Tell the user their location, inventory and prompt for a move, ignores case
print('You are in the ' + location)
print(inventory)
# tell the user if there is an item in the exhibit
if location != 'Security Headquarters' and 'item' in exhibits[location].keys():
print('You see the {}'.format(exhibits[location]['item']))
print('------------------------------')
move = input('Give code for next move: ').title().split()
# handle if the user enters a command to move to a new exhibit
if len(move) >= 2 and move[1] in exhibits[location].keys():
location = move_between_rooms(location, move[1], exhibits)
continue
# handle if the user enter a command to get an item
elif len(move[0]) == 3 and move[0] == 'Get' and ' '.join(move[1:]) in exhibits[location]['item']:
print('You pick up the {}'.format(exhibits[location]['item']))
print('------------------------------')
get_item(location, move, exhibits, inventory)
continue
# handle if the user enters an invalid command
else:
print('INVALID CODE.\n')
print('TRY AGAIN.')
continue
main()
Upvotes: 1
Views: 56
Reputation: 501
Interesting game. Does your code assume that indexing lists in Python is zero based? From the code, moving between locations is handled by if len(move) >= 2 and move[1] in exhibits[location].keys():
in the main function. My interpretation of reading this condition is that it expects at least two elements. The direction of the move is expected to be in the second element of the list move[1]
. Because of the way move
is parsed from the input, entering something like dummy S
will work, but entering S
gives the error you are seeing.
I propose two solutions.
if len(move) >= 1 and move[0] in exhibits[location].keys():
Upvotes: 1