Created
August 28, 2019 12:22
-
-
Save cherylli/bb3b506cf77e74332ae6e0a1442d78af to your computer and use it in GitHub Desktop.
Automate the Boring Stuff with Python Chapter 5 Practice Projects - Fantasy Game Inventory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def displayInventory(inv): | |
print("Inventory: ") | |
total = 0 | |
for item, quantity in inv.items(): | |
print(f'{quantity} {item}') | |
total += quantity | |
print(f'Total number of items: {total}') | |
def addToInventory(inventory, addedItems): | |
inv_copy = inventory.copy() | |
for item in addedItems: | |
inv_copy[item] = inv_copy.get(item, 0)+1 | |
return inv_copy | |
inventory = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12} | |
displayInventory(inventory) | |
inv = {'gold coin': 42, 'rope': 1} | |
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby'] | |
inv = addToInventory(inv, dragonLoot) | |
displayInventory(inv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment