This is the code I have, I am just trying to fix the inventory/get item feature and
Question:
This is the code I have, I am just trying to fix the inventory/get item feature and I cannot figure out where I have messed up
def show_instructions():
print('Welcome to Evil Sorcerer Text Adventure Game')
print('Collect the 6 items to win the game, or be defeated by the evil wizard')
print('Move commands: North, South, East, and West')
print('To add to inventory: "item name" ')
rooms = {
'Living Room': {'North': 'Garage', 'South': 'Library', 'East': 'Courtyard', 'West': 'Garden'},
'Garage': {'South': 'Living Room', 'East': 'Kitchen', 'Item': 'Saddle'},
'Kitchen': {'West': 'Garage', 'Item': 'Healing Potion'},
'Garden': {'East': 'Living Room', 'Item': 'Goat'},
'Library': {'North': 'Living Room', 'East': 'Bedroom', 'Item': 'Spell Book'},
'Bedroom': {'West': 'Library', 'Item': 'Wand'},
'Courtyard': {'North': 'Lighthouse', 'West': 'Living Room', 'Item': 'Dragon'},
'Lighthouse': {'South': 'Courtyard'}
}
DIRECTIONS = ['North', 'South', 'East', 'West']
EXIT_COMMAND = "Exit"
VALID_INPUTS = DIRECTIONS + [EXIT_COMMAND]
INVALID_DIRECTION = "That is not a valid direction. You need to enter one of: " + \
str(VALID_INPUTS) + "."
EXIT_ROOM_SENTINEL = "Exit"
CANNOT_GO_THAT_WAY = "You bumped into a wall."
GAME_OVER = "Thanks for playing."
def navigate(current_room: str, user_command: str):
"""
Given a current_room in rooms and a user_input, return a tuple (next_room, err_msg) with
next_room -- where you are after or EXIT_ROOM_SENTINEL
err_msg -- message to print, if any, empty, GAME_OVER, INVALID_DIRECTION, or CANNOT_GO_THAT_WAY
"""
next_room = current_room
err_msg = ''
if user_command in rooms[current_room]:
# If the direction the user wants to go is in rooms[current_room]
next_room = rooms[current_room][user_command]
# It will assign the next room as rooms[current_room][user_input]
elif user_command == EXIT_COMMAND:
# If the user wants to exit the game
return EXIT_ROOM_SENTINEL, GAME_OVER
# It will give the user the exit message
elif user_command not in DIRECTIONS:
# If the user input is not a valid direction
return current_room, INVALID_DIRECTION
# It will show up as the invalid direction error message
elif user_command not in rooms[current_room]:
# If there is no room in the direction the user wants to go
return current_room, CANNOT_GO_THAT_WAY
# It will show up as the wall error message
return next_room, err_msg
def get_item(current_room: str, user_item: str):
inventory = []
if user_item in rooms[current_room]:
inventory.append(rooms[current_room][user_item])
return inventory
def main():
# Main gameplay
show_instructions()
while True:
# Starts the loop for the gameplay
current_room = 'Living Room'
# Assigns 'Living Room' as the current room
print(f'You are in the {current_room}.')
# Tells the user where they are
user_input = input('Enter a direction(North, South, East, or West) or type Exit to exit the game:').capitalize()
# Prompts user to enter either a direction or exit and capitalizes the answer
next_room, err_msg = navigate(current_room, user_input)
# Unpacks the returned tuple so that it can be used
if err_msg == GAME_OVER:
# Checks if the error message is 'GAME_OVER'
print(GAME_OVER)
# Prints the 'GAME_OVER' message
break
# Stops the game
elif err_msg:
# For any other error message
print(err_msg)
# Just prints that error message
else:
current_room = next_room
# Assigns the current room as the next room
print(f'You are in the {current_room}.')
while current_room != 'Living Room' or 'Lighthouse':
user_item = input('Do you want to grab the item? type the item name:').capitalize()
inventory: list[str] = get_item(current_room, user_item)
print(inventory)
if current_room == 'Lighthouse':
if len(inventory) == 6:
print('You collected all the items and defeated The Evil Sorcerer')
print('Your movie night has been saved!')
print('Thanks for playing!')
break
else:
print('You did not collect all of the items and The Evil Sorcerer defeated you!')
print('Your movie night was ruined.')
print('Thanks for playing!')
break
if __name__ == "__main__":
main()
# Runs the game