-2

When i enter a room, i am prompted with an item to be picked up and added to my inventory, when i enter a room with a one word item, i can pick it up no problem, but when i go into a room with a two word item, i cannot pick it up and add it to my inventory. I feel like i'm missing something obvious, but i can't figure it out, i've been on this for a while and finally have folded and am asking for help.

def display_instructions():
    print("++++++++++++++++++++")
    print("You are a village witch doctor and an villager is sick. "
          "Collect the ingredients for a potion to cure the sick villager")
    print("++++++++++++++++++++")
    print("Use go North, go South, go East, go South to navigate "
          "different sections of the forest. Use get, with item name after to pick up item.")
    print("++++++++++++++++++++")


def display_room(room, items):
    print("You are in the " + room + ".")
    if len(items) > 0:
        print("In this room, you see the following items: ")
        for item in items:
            print("- " + item)
    else:
        print("There are no items in this room.")
        print("++++++++++++++++++++")


def display_inventory(inventory):
    if len(inventory) > 0:
        print("Your inventory contains the following items: ")
        for item in inventory:
            print("- " + item)
    else:
        print("Your inventory is empty.")


def get_input():
    return input("What would you like to do? ")


def play_game():
    rooms = {
        "House": {"North": "North Forest", "South": "South Forest"},
        "North Forest": {"East": "NE Forest", "South": "House"},
        "NE Forest": {"East": "North River", "West": "North Forest"},
        "North River": {"South": "Central River", "West": "NE Forest"},
        "Central River": {"South": "South River", "North": "North River"},
        "South River": {"North": "Central River", "West": "SE Forest"},
        "SE Forest": {"West": "South Forest", "East": "South River"},
        "South Forest": {"North": "House", "East": "SE Forest"}
    }

    items = {
        "Lilyroot": "North River",
        "Pinecone": "North Forest",
        "Nest Wax": "NE Forest",
        "Feather": "South Forest",
        "Green Mushroom": "South River",
        "Bee Nectar": "SE Forest"
    }

    current_room = "House"
    inventory = []

    display_instructions()

    while True:
        display_room(current_room, [item for item, room in items.items() if room == current_room])
        display_inventory(inventory)

        user_input = get_input()
        command_parts = user_input.lower().split()

        if command_parts[0] == "go":
            if len(command_parts) == 2:
                direction = command_parts[1].capitalize()
                if direction in rooms[current_room]:
                    current_room = rooms[current_room][direction]
                else:
                    print("Invalid Direction.")
                    print("++++++++++++++++++++")
            else:
                print("Invalid command. Please enter 'go [direction]'.")
# Here
        elif command_parts[0] == "get":
            if len(command_parts) == 2:
                item_name = command_parts[1].capitalize()
            elif len(command_parts) == 3:
                item_name = command_parts[2].capitalize()
                if item_name in items and items[item_name] == current_room:
                    inventory.append(item_name)
                    print("You got " + item_name + "!")
                    del items[item_name]
                else:
                    print("Incorrect Item Name")
            else:
                print("Invalid command. Please enter 'get [item name]'.")
        else:
            print("Invalid command. Please enter 'go [direction]' or 'get [item name]'.")

        if current_room == "House" and (len(items) == 0):
            print("The potion can now be crafted, the villager is saved!!!!")


if __name__ == "__main__":
    play_game()

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    I would try checking the documentation for the `string.split()` function – Gordon Gustafson Aug 12 '23 at 21:03
  • 1
    If `len(command_parts) == 3`, what do you expect `command_parts[2]` to be? – slothrop Aug 12 '23 at 21:04
  • @slothrop i'm trying to get the 2 word dictionary entry as an item in my inventory, `len(command_parts) == 2` pulls one the one word items so `len(command_parts) == 3` pulls the two word items, as for `command_parts[2]` i was trying to match to the above if statement but for two words. i am unsure how to merge the two list items while keeping a space with the formatting i am using – redisbest Aug 12 '23 at 21:20
  • Welcome to Stack Overflow! Please take the [tour]. SO is a Q&A site, but there's no question here and it's not clear what exactly you need help with. You've got a good description of the problem, now add details: What inputs do you give to the program, and what output do you get? What were you expecting to get instead? Also, this is too much code. Please remove anything that's not relevant to the problem, like `display_instructions` to start. See [mre]. Once you've isolated the problem, then you should be able to ask a specific question about it. You can [edit]. – wjandrea Aug 12 '23 at 21:42
  • For more tips, see [ask] and [How to ask and answer homework questions](//meta.stackoverflow.com/q/334822/4518341) specifically. And if it helps: [How to step through Python code to help debug issues?](/q/4929251/4518341) – wjandrea Aug 12 '23 at 21:42
  • 1
    @redisbest *"i am unsure how to merge the two list items while keeping a space"* -- Have you considered just `command_parts[1] + " " + command_parts[2]`? There are better ways to do that, to be clear, but that's the simplest. – wjandrea Aug 12 '23 at 21:48

0 Answers0