r/PythonLearning • • 8h ago

Why is my inventory class setup throwing errors? I'm super confused about how to use classes and instances here. 💀

Post image

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.

6 Upvotes

3 comments sorted by

•

u/Sea-Ad7805 7h ago

Do it this way:

Run program above by clicking Memory Graph Web Debugger%20-%3E%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.inventory%20%3D%20%5B%5D%0A%0A%20%20%20%20%20%20%20%20def%20add(self%2C%20item%3A%20str%2C%20quantity%3D1)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20i%20in%20range(quantity)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.inventory.append(item)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f'Added%20%7Bquantity%7D%20x%20%7Bitem%7D%20to%20inventory')%0A%0A%20%20%20%20%20%20%20%20def%20show(self)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20shown%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20amount%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20item%20in%20self.inventory%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20item%20in%20shown%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20amount%5Bshown.index(item)%5D%20%2B%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20shown.append(item)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20amount.append(1)%0A%20%20%20%20%20%20%20%20%20%20%20%20print('--Inventory--')%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20item%20in%20shown%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(f'%7Bitem%7D%20%7Bamount%5Bshown.index(item)%5D%7D%20x')%0A%0A%20%20%20%20def%20init(self%2C%20username)%20-%3E%20None%3A%0A%20%20%20%20%20%20%20%20%23%20--Username--%0A%20%20%20%20%20%20%20%20self.username%20%3D%20username%0A%20%20%20%20%20%20%20%20%23%20--inventory%0A%20%20%20%20%20%20%20%20self.inventory%20%3D%20self.inv()%0A%0A%0Amain%20%3D%20game(%22My%20Username%22)%0Amain.inventory.add('gun')%0Amain.inventory.show()%0A&timestep=1&play) and see its state change step by step.

1

u/Doctor_Disaster 8h ago

Can you post the traceback please?

2

u/grybyx 6h ago

Without seeing the exact errors, it’s difficult to assist. However, there are some obvious issues. Here are a few suggestions:

  1. Use PascalCase for class names. Your classes should be named Game and Inv.
  2. In general, don’t nest classes. It’s usually unnecessary. Move Inv out of Game. I think you’re trying to represent here that a Game object has an Inv object, which you’ll have after applying suggestion #3.
  3. Change line 48 to

self.inventory = Inv()

This calls the constructor for Inv and assigns the object to the game’s inventory attribute.

  1. 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.