Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I can not get the get_item function to work, everything else is working. def main_menu(): # Print instructions print('Telegram ') print('Your are to proceed to

I can not get the get_item function to work, everything else is working.

def main_menu():

# Print instructions

print('Telegram ')

print('Your are to proceed to the village of Rio Negro in the bad lands of New Mexico and capture '

'the notorious outlaw, Black Bart. Upon arrival, there are a few items that you must retrieve '

'from different place in the village, to aid you in the capture of Black Bart. '

'Your item list includes shackles, ammunition, medicine, a hotel room key, Arrest Warrant, '

'and a last will and testimony. '

'Good luck to you Marshall. ')

print('Move commands: go north, go south, go east, go west')

print('Add item to saddle bag: get "item name" ')

print('P.S. If you decide that you are not up to the task of capturing the fugitive, feel free to sneak '

'out of town by way of the back door of the hotel, located on the west wall. ')

def move_between_rooms(current_room, move, rooms):

# move to corresponding room

current_room = rooms[current_room][move]

return current_room

def get_item(current_room, move, rooms, inventory):

# add item to inventory and remove it from the room

inventory.append(rooms[current_room]['item'])

del rooms[current_room]['item']

# The dictionary links a room to other rooms.

rooms = {

'Village Square': {'North': "Doctor's office", 'South': "Lawyer's office", 'East': 'Mercantile', 'West': 'Hotel'},

'Hotel': {'East': 'Village Square', 'West': 'Exit', 'item': 'room key'},

"Doctor's office": {'South': 'Village Square', 'East': 'Blacksmith', 'item': 'Medicine'},

"Lawyer's office": {'North': 'Village Square', 'East': 'Jail', 'item': 'Last Will and Testimony'},

'Jail': {'West': "Lawyer's office", 'item': 'arrest warrant'},

'Mercantile': {'North': 'Tavern', 'West': 'Village Square', 'item': 'ammunition'},

'Blacksmith': {'West': "Doctor's office", 'item': 'shackles'},

'Tavern': {'South': 'Mercantile'},

'Exit': {'East': 'Hotel'}

}

s = ' '

# list for storing player inventory

inventory = []

# starting point

current_room = 'Village Square'

# display main_menu

main_menu()

while True:

# handle the case when player encounters the 'villain'

if current_room == 'Tavern':

# winning case

if len(inventory) == 0:

print('Black Bart: "Well, look what the cat dragged in, another lost Marshall! I '

'guess I will have to point him in the right direction!"')

print('Marshall: "The only thing you will be showing me is how quickly you can put '

'these shackles on!" ')

print('Black Bart: "Oh, no!! I guess I will have to do what you say! ')

print('Thank you for playing!')

break

# losing case

else:

print('Black Bart: "You yellow bellied son of a motherless goat!!" '

'"How dare you come here without the proper equipment!!"')

print('"Prepare to meet your maker!!"')

print('Bang, bang!!')

print('Your dead!!')

print('Thank you for playing!')

break

# Tell the user their current room, inventory and prompt for a move, ignores case

print('You are in the ' + current_room)

print(inventory)

# tell the user if there is an item in the room

if current_room != 'Tavern' and 'item' in rooms[current_room].keys():

print('You see the {}'.format(rooms[current_room]['item']))

print('------------------------------')

move = input('Enter your move: ').title().split()

# handle if the user enters a command to move to a new room

if len(move) >= 2 and move[1] in rooms[current_room].keys():

current_room = move_between_rooms(current_room, move[1], rooms)

continue

# handle if the user enter a command to get an item

elif len(move) == 4 and move[0] == 'get' and s.join(move[1:3]) in rooms[current_room]['item']:

print('You pick up the {}'.format(rooms[current_room]['item']))

print('------------------------------')

get_item(current_room, move, rooms, inventory)

continue

# handle if the user enters an invalid command

else:

print('Invalid move, please try again')

continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions

Question

3. Why are collusions and cartels often unstable?

Answered: 1 week ago