zeroMana
zeroMana

Reputation: 1

Need help adding full value in dictionary to inventory in my text-based game

I have a key:value pair == 'item': energy potion. I'm trying to add the whole value energy potion to my inventory but only 'energy' gets added. I know it has to do with obj = command.lower().split()[1] as this just adds the value at location 1. I don't know how to get both words.

import time

def instructions():
    directions = 'North, East, South, or West'
    print(f'\nYou may go {directions}.')
    print('\nEquip item: get \'item name\'')
    print('\nTo exit, type \'quit\'.')


def player_status(rooms, current_room, inventory):
    print(f'\nYou are at {current_room}.')
    if 'item' in rooms[current_room]:
        print('There is a ' + rooms[current_room]['item'])
    print('You have:', inventory)


def main():
    inventory = []
    rooms = {
        'your apartment': {'West': 'occult bookstore', 'South': 'McDally\'s Pub'},

        'occult bookstore': {'East': 'your apartment', 'item': 'Skull'},

        'McDally\'s Pub': {'North': 'your apartment', 'East': 'Shedd Aquarium', 'South': 'Wrigley Field',
                           'item': 'energy potion'},

        'Wrigley Field': {'North': 'McDally\'s Pub', 'West': 'your office', 'item': 'wizard\'s staff'},

        'your office': {'East': 'Wrigley Field', 'item': 'Faerie Queen'},

        'Shedd Aquarium': {'West': 'McDally\'s Pub', 'North': 'Field Museum of Natural History',
                           'item': 'enchanted duster'},

        'Field Museum of Natural History': {'South': 'Shedd Aquarium', 'North': 'Saint Mary of the Angels church',
                                            'item': 'blasting rod'},

        'Saint Mary of the Angels church': {'South': 'Field Museum of Natural History', 'item': 'shield bracelet'}
    }

    current_room = 'your apartment'

        instructions()
        player_status(rooms, current_room, inventory)
        time.sleep(3)

        # sets up user command to move player between rooms.
        command = input('\nWhat do you want to do?\n>').title().strip()
        if command in rooms[current_room]:
            current_room = rooms[current_room][command]
            time.sleep(1)

        # to move item into inventory and remove it from dict.
        elif command.lower().split()[0] == 'get':
            obj = command.lower().split()[1]
            if obj in rooms[current_room]['item']:
                del rooms[current_room]['item']
                inventory.append(obj)
            else:
                print("I don't see that here.")

        # a command to quit the game.
        elif command.lower() in ('q', 'quit'):
            time.sleep(1)
            print('\nThanks for playing.')
            break
        
        # if player inputs an invalid command.
        else:
            time.sleep(1)
            print('\nYou can\'t go that way!')
        


if __name__ == "__main__":
    main()

here is how the game runs at current. Any help would be greatly appreciated!

You are at McDally's Pub.
There is a energy potion
You have: []

What do you want to do?
>get energy potion

You may go North, East, South, or West.

Equip item: get 'item name'

To exit, type 'quit'.

You are at McDally's Pub.
You have: ['energy']

What do you want to do?
>

Update: I have made changes with maxsplit=1 but now I cannot add an item with only one value to inventory. ex. skull cannot be added.

   elif command.lower().split()[0] == 'get':
       obj = command.lower().split(maxsplit=1)[1]
       if obj in rooms[current_room]['item']:
           del rooms[current_room]['item']
           inventory.append(obj)
       else:
           print("I don't see that here.")

code outputs:

You are at occult bookstore.
There is a Skull
You have: ['energy potion']

What do you want to do?
>get skull
I don't see that here.

Upvotes: 0

Views: 56

Answers (1)

John Gordon
John Gordon

Reputation: 33335

Use the optional maxsplit=1 argument to split, so that it will only split on the first space. Any spaces after that will not be split.

If command is "get energy potion", then command.split(maxsplit=1) will return ['get', 'energy potion'].

Upvotes: 1

Related Questions