DEV Community


Posted on

Inventory for Text-based Adventure Game in Python.

Want to make an inventory for a text-based python game. Well it's as easy as making a list. To make a list, at the start of your code write something like inventory = []. This will create an empty list. You are now all done! You've made your inventory. When your character picks up an item you can add it to your list with inventory.append(). This function adds an item to the end of a list. If you want to remove an item from the inventory, you can use the inventory.remove() function.

Here is a quick example of how to use an inventory system to open up a locked door.

inventory = []

def game():

    print("You are standing at a locked door and there is a key on the ground.")
    in_room = True

    while in_room == True: 
        action = input("What do you do?: ").lower().strip()

        if action == "pick up key":
            print("You picked up the key and added it to your inventory")

        elif action == "open door" and ("key") not in inventory:
            print("The door is locked")

        elif action == "open door" and ("key") in inventory:
            print("The door opens")
            in_room = False

            print("I do not understand")
        print("Congrat! you made it out!")
Enter fullscreen mode Exit fullscreen mode

If you are making a game in python. Let me know I'd love to check it out.

Discussion (1)

paramjot0 profile image

thanks, this helped