0

So far I have been able to add either one-word values or 2-word values but not both. For example, I want to go to one location and add a skull to my inventory, then move to another location and add an energy potion to my inventory. With my current code, I can add the energy potion by using .split(maxsplit=1) or .split(' ', 1). That was to solve the issue of not being able to add a value with 2 words. But this seems to prevent adding values with one word to the inventory.

while True:
    instructions()
    player_status(rooms, current_room, inventory)
    time.sleep(0.5)

    # 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(0.5)

    # command to add item to inventory and remove it from dict.
    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.")

    # a command to quit the game.
    elif command.lower() in ('q', 'quit'):
        time.sleep(0.5)
        print('\nThanks for playing.')
        break

    # if player inputs an invalid command.
    else:
        time.sleep(0.5)
        print('\nYou can\'t go that way!')

Here is an example of the output:

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.
Michael M.
  • 10,486
  • 9
  • 18
  • 34
zeroMana
  • 1
  • 2
  • 1
    Seems to me that you have a 'Skull' in the room, but you are trying to pick up a 'skull'. `obj = command.lower().split(maxsplit=1)[1]` converts everything to lower case. But your script prompts with `There is a Skull`, suggesting it is stored with a capital in your room inventory. – Galo do Leste Feb 12 '23 at 23:30
  • You are a lifesaver! I probably never would have caught that. Thanks. Everything works now. – zeroMana Feb 12 '23 at 23:45

1 Answers1

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

That's searching for skull but Skull doesn't match. You could use [for a case-insensitive search]

        if obj.lower() in [i.lower() for i in rooms[current_room]['item']]:

but that doesn't feel very efficient. It might be better to just apply .title() or .capitalize() to obj and make sure that all items in rooms[current_room]['item'] are in consistent format.

Driftr95
  • 4,572
  • 2
  • 9
  • 21