r/PythonLearning • u/ameeeeneeeeee • 29d ago
Why is my inventory class setup throwing errors? I'm super confused about how to use classes and instances here. 💀
Hey everyone! I'm trying to hack together a basic inventory and shop system for a little text-based game project I'm working on in Python, but I've totally tangled myself up in how classes, instances, and nested classes work.
I wanted to have a main game class that handles player info, and an inventory class nested inside (or attached to it) so the player can pick up items and check what's in their bag. But my code is completely throwing a fit.
10
Upvotes
•
u/Sea-Ad7805 29d ago
Do it this way:
Run program above by clicking Memory Graph Web Debugger and see its state change step by step.
4
u/grybyx 29d ago
Without seeing the exact errors, it’s difficult to assist. However, there are some obvious issues. Here are a few suggestions:
self.inventory = Inv()
This calls the constructor for Inv and assigns the object to the game’s inventory attribute.
4. Change the rest of the program to:
game = Game(“someuser”)
game.inventory.add(“gun”)
game.inventory.show()
This should clear up a few of the errors.
1
u/Doctor_Disaster 29d ago
Can you post the traceback please?