Below is my code for the Fantasy Game Inventory problem from Chapter 5 of Automate the Boring Stuff with Python. Is there anything I can do to make it cleaner or more efficient?
The code starts out with a dictionary "inv" that contains the player's current inventory as well as a list "dragon_loot" that contains the items from the dead dragon. The goal of the code is to combine the list with the dictionary while increasing the quantity of duplicate items.
As a note, I am using the starting list from the previous question simply because it seems impossible to kill a dragon without possessing a weapon :)
You are creating a fantasy video game. The data structure to model the player’s inventory will be a dictionary where the keys are string values describing the item in the inventory and the value is an integer value detailing how many of that item the player has. For example, the dictionary value {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12} means the player has 1 rope, 6 torches, 42 gold coins, and so on.
List to Dictionary Function for Fantasy Game Inventory
Imagine that a vanquished dragon’s loot is represented as a list of strings like this:
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
Write a function named addToInventory(inventory, addedItems), where the inventory parameter is a dictionary representing the player’s inventory (like in the previous project) and the addedItems parameter is a list like dragonLoot. The addToInventory() function should return a dictionary that represents the updated inventory. Note that the addedItems list can contain multiples of the same item. Your code could look something like this:
def addToInventory(inventory, addedItems):
# your code goes here
inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv = addToInventory(inv, dragonLoot)
displayInventory(inv)
The output of the following code is:
Inventory:
1 rope
6 torch
45 gold coin
2 dagger
12 arrow
1 ruby
Total number of items: 67
Thank you in advance. I have learned a lot from the comments I received for my previous questions!
from collections import Counter
# players current inventory before killing dragon
inv = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
# items from the slain dragon
dragon_loot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
# this function prints the players inventory in a list and reports the total items
def display_inventory(inventory):
print("Inventory:")
item_total = 0
for k, v in inventory.items():
print(str(v) + " " + str(k))
item_total = item_total + int(v)
print("\nTotal number of items: " + str(item_total))
# this function adds the dragon's stuff to the players inventory
def add_to_inventory(inventory, added_items):
added_items_dict ={i:added_items.count(i) for i in added_items}
inv = Counter(inventory) + Counter(added_items_dict)
return inv
inv = add_to_inventory(inv, dragon_loot)
display_inventory(inv)