Skip to content
← Back to Community
How to make inventory system
Profile icon
Bookie0

Hello,

How do you make an inventory system in Python? I’m thinking of having the inventory only contain 1 item in the start, like the fists. But then, as the character progresses in the game, they can get more items.

So the question is, first of all, how do you make that inventory, and second of all, how would you add items inside that inventory, and third of all, how do you print the item that the user can choose with an input (so like i would show what items they have, and the user then choose out of the items which one they want to use)

Please bear in mind I am a kinda intermediate coder, so I might require you to explain.

Thank you very much in advance!

Edit: someone already helped me but if you have tips why not

Voters
Profile icon
24aortega
Profile icon
Bookie0
Comments
hotnewtop
Profile icon
CoolJames1610

yeah just use dicts

Profile icon
[deleted]

Here is a less complicated version (For me, Im kinda a noob)

Inventory = ["Item 1"] #when character gets item Inventory.append("Item 2") print(Inventory) #character picks item choice = input("Which Item do you choose?:") if choice == 'Item 1': print("You chose item 1") elif choice == 'Item 2': print("Item 2")
Profile icon
Bookie0

Thanks for the suggestion @CarlosRosiles

Profile icon
CodeLongAndPros

You could do:

import ast inv = {} inv["cats"] = 100 with open('data', 'w') as f: f.write(str(inv)) del inv with open('dat') as f: inv = ast.literal_eval(f.read())
Profile icon
Bookie0

Thanks for the suggestion @CodeLongAndPros

Profile icon
CodeLongAndPros

@Bookie0 Sure.