From 5a09f0a722055ab1c38c9faf6be2480d0d219431 Mon Sep 17 00:00:00 2001 From: Ninjdai1 Date: Fri, 13 Oct 2023 16:02:54 +0200 Subject: [PATCH] Allow iteration on Inventory object --- index.py | 3 +++ src/inventory/inventory.py | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/index.py b/index.py index a8ce3cd..7525db6 100644 --- a/index.py +++ b/index.py @@ -34,6 +34,9 @@ def start(self) -> None: sword.destroy() print(game.player.inventory, "\n") + for item in game.player.inventory: + print(itemk) + #coins.count = -42 #save(game, "game") diff --git a/src/inventory/inventory.py b/src/inventory/inventory.py index 7847003..3cff075 100644 --- a/src/inventory/inventory.py +++ b/src/inventory/inventory.py @@ -21,7 +21,15 @@ def _on_item_destroyed(self, item) -> None: """Removes inputted item from the inventory""" self._items.remove(item) + def __iter__(self): + self.iter_data = self._items[:] + return self + + def __next__(self): + if not self.iter_data: + raise StopIteration + return self.iter_data.pop() + def clear(self) -> None: """Clears the inventory""" self._items = [] -