Search code examples
pythoncode-formatting

How to put item from room into inventory


I am having issues with not being able to get any item and move it into my inventory on this text-based game, I have tried a couple of things but none of it has helped and I am genuinely stuck. I am creating is there any idea or solution on what I need to do or what I am missing? Please Help

def user_instructions():
    print('--------------')
    print('You are a monkey and wake up to discover your tribe is under attack by the Sakado tribe ')
    print('Your goal is to collect all 6 items and bring them to the Great Mother Tree to save the tribe!')
    print('Their life is in your hands!')
    print('\nMove through the rooms using the commands: "north", "east", "south", or "west"')
    print('Each room contains an item to pick up, use command: "(item name)"')
    print('\nDo not failure your tribe!')


# define command available for each room
rooms = {
    'Great Hall': {'east': 'Shower Hall', 'south': 'Armory Room', 'west': 'Bedroom', 'north': 'Chow Hall', 'item': 'Armor of the Hacoa Tribe'},
    'Bedroom': {'east': 'Great Hall', 'item': 'Tribe Map'},
    'Chow Hall': {'east': 'Bathroom', 'south': 'Great Hall', 'item': 'Golden Banana'},
    'Shower Hall': {'west': 'Great Hall', 'north': 'Branding Room', 'item': 'Sword of a 1000 souls'},
    'Bathroom': {'west': 'Chow Hall', 'item': 'None'},
    'Branding Room': {'south': 'Shower Hall', 'item': 'Sacred Key'},
    'Armory Room': {'north': 'Great Hall', 'east': 'Great Mother Tree', 'item': 'Spear of the Unprotected'},
    'Great Mother Tree': {'west': 'Armory'}
}


def user_status():  # indicate room and inventory contents
    print('\n-------------------------')
    print('You are in the {}'.format(current_room))
    print('In this room you see {}'.format(rooms[current_room]['item']))
    print('Inventory:', inventory_items)
    print('-------------------------------')


def invalid_move():
    print('Command not accepted, please try again')


def invalid_item():
    print('Item is not found in this room')
    user_status()


def show_status(current_room, inventory, rooms):
    print('   -------------------------------------------')
    print('You are in the {}'.format(current_room))
    print('Inventory:', inventory_items)
    print('   -------------------------------------------')


user_instructions()

inventory_items = []  # list begins empty
current_room = 'Bedroom'  # start in bedroom
command = ''

while current_room != 'Great Mother Tree':  # Great Mother Tree is the end of the game, no commands can be entered
    user_status()
    command = input('Enter your next move.\n').lower()
    if command == 'get':
        item = input('What do you want to take? ')
        if item in rooms(current_room):
            inventory_items.append(item)
        else:
            print(f"There's no {item} here.")
    elif command in rooms[current_room]:
        current_room = rooms[current_room][command]
    else:
        print('Invalid command')

if len(inventory_items) != 6:
    print('You Lose')

else:
    print('you win')

Solution

  • Firstly, you are using round parentheses to access the dictionary here:

    if item in rooms(current_room):
    

    which causes an error.

    If you replace it with:

    if item in rooms[current_room]:
    

    it still won't work because you need to access item key in the sub-dictionary. So, it should be:

    if item in rooms[current_room]['item']: