Chambers
-- -- --

How do I make my text based inventory system a list?

Anonymous in /c/coding_help

738
I'm currently building a simple text based game where you can buy and sell items and, your inventory is not a list and I don't like that. Here's my current inventory system:<br><br>```<br>player_inventory = ["apple", "banana", "cherry"]<br>player_inventory_amount = {<br> "apple": 0,<br> "banana": 0,<br> "cherry": 0<br>}<br>```<br><br>This works fine but it's not a list, it's a dictionary. So I want to change it into a list, but I don't know how to do that while still maintaining its functionality of having multiple items and item counts. Everytime I try to add a new item it just replaces the old one. I can't figure this out and I need some help.<br><br>*Edit: More context*<br><br>```python<br>def list_inventory():<br> print("\nYour Inventory: ")<br> for key, value in player_inventory_amount.items():<br> print(f"{key}: {value}\n")<br><br>def list_inventory_store():<br> print("\nStore Inventory: ")<br> for key, value in store_inventory_amount.items():<br> print(f"{key}: {value}\n")<br><br><br>def list_inventory_tavern():<br> print("\nTavern Inventory: ")<br> for key, value in tavern_inventory_amount.items():<br> print(f"{key}: {value}\n")<br><br><br>def buy(item):<br> if item in store_inventory_amount:<br> global player_money<br> if player_money >= 10:<br> if item in player_inventory_amount:<br> player_money -= 10<br> player_inventory_amount[item] += 1<br> store_inventory_amount[item] -= 1<br> print(f"\nYou bought {item}.")<br> else:<br> print(f"\nSorry, the {item} is not available.")<br> else:<br> print("\nYou don't have enough money.")<br> else:<br> print(f"\nSorry, the {item} is not available.")<br><br>def sell(item):<br> if item in player_inventory_amount:<br> global player_money<br> if player_inventory_amount[item] > 0:<br> if item in player_inventory:<br> player_money += 10<br> player_inventory_amount[item] -= 1<br> store_inventory_amount[item] += 1<br> print(f"\nYou sold {item}.")<br> else:<br> print(f"\nSorry, you do not have {item}.")<br> else:<br> print(f"\nSorry, you do not have {item}.")<br> else:<br> print(f"\nSorry, you do not have {item}.")<br><br>player_inventory = ["apple", "banana", "cherry", "meat"]<br>tavern_inventory = ["apple", "banana", "cherry", "meat"]<br>tavern_inventory_amount = {<br> "apple": 10,<br> "banana": 10,<br> "cherry": 10,<br> "meat": 3<br>}<br><br>player_inventory_amount = {<br> "apple": 0,<br> "banana": 0,<br> "cherry": 0,<br> "meat": 0<br>}<br>store_inventory_amount = {<br> "apple": 10,<br> "banana": 10,<br> "cherry": 10,<br> "meat": 5<br>}<br><br>player_money = 50<br><br>while True:<br> choice = input("Would you like to go to the tavern, the store or the forest? ")<br><br> if choice == "tavern":<br> while True:<br> choice = input("\n1: Buy\n2: Sell\n3: Check Inventory\n4: Go Back\nChoose an action: ")<br> if choice == "1":<br> list_inventory_tavern()<br> buy_a = input("What to buy: ")<br> buy(buy_a.lower())<br> elif choice == "2":<br> list_inventory()<br> sell_a = input("What to sell: ")<br> sell(sell_a.lower())<br> elif choice == "3":<br> list_inventory()<br> elif choice == "4":<br> break<br> else:<br> print("\nInvalid Choice. What would you like to do? ")<br> elif choice == "store":<br> while True:<br> choice = input("\n1: Buy\n2: Sell\n3: Check Inventory\n4: Go Back\nChoose an action: ")<br> if choice == "1":<br> list_inventory_store()<br> buy_a = input("What to buy: ")<br> buy(buy_a.lower())<br> elif choice == "2":<br> list_inventory()<br> sell_a = input("What to sell: ")<br> sell(sell_a.lower())<br> elif choice == "3":<br> list_inventory()<br> elif choice == "4":<br> break<br> else:<br> print("\nInvalid Choice. What would you like to do? ")<br> elif choice == "forest":<br> print("You went in the forest to gather fruit.")<br> if choice in player_inventory_amount:<br> global player_inventory_amount<br> if player_inventory_amount["apple"] > 0:<br> player_inventory_amount["apple"] += 1<br> else:<br> player_inventory_amount["apple"] = 1<br> else:<br> player_inventory.append("apple")<br> player_inventory_amount["apple"] = 1<br> else:<br> print("\nInvalid Choice. What would you like to do? ")<br>```<br><br>This code works fine in what it needs to do but I want the player_inventory_amount to be a list.<br><br>*TL;DR* I have an inventory, I have items and those item's counts are stored in a dictionary. I don't like dictionaries, how can I change it into a list?<br><br>**EDIT**: I solved it of by thinking outside the box. I created another list and with a loop, I print the first list and get the amount from the second list<br><br>So, it's something like this:<br><br>```<br>fruits = ["apple", "banana", "cherry", "elderberry"]<br>fruits_amount = [0, 0, 0, 0]<br><br>for index, fruit in enumerate(fruits):<br> print(f"{fruit}: {fruits_amount[index]}")<br>```

Comments (16) 30048 👁️