Skip to content

Instantly share code, notes, and snippets.

@toonarmycaptain
Created July 27, 2017 20:07
Show Gist options
  • Save toonarmycaptain/22d6f14681c4cf11dea2044df54a0861 to your computer and use it in GitHub Desktop.
Save toonarmycaptain/22d6f14681c4cf11dea2044df54a0861 to your computer and use it in GitHub Desktop.
Automate the Boring Stuff Chpt 5 - Fantasy Game Inventory
stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
#def displayInventory(inventory):
# total_items = 0
# for item in inventory:
# print(str(inventory[item])+' '+item)
# total_items += inventory[item]
# print("Total number of items: "+str(total_items))
# This worked but I like the following better:
def displayInventory(inventory):
total_items = 0
for item, quantity in inventory.items():
print(str(quantity)+' '+item)
total_items += quantity
print("Total number of items: "+str(total_items))
displayInventory(stuff)
def addToInventory(inventory, addedItems):
for item in addedItems:
inventory.setdefault(item, 0) #this adds a (defaulted to zero value) key to the inventory dict if it's not already there
inventory[item] += 1 #and this increases that value by one, each time that item appears in the loot list
return inventory
inv = {'gold coin': 42, 'rope': 1}
displayInventory(inv)
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv = addToInventory(inv, dragonLoot)
displayInventory(inv)
@muning24
Copy link

thank you <3

@solmv
Copy link

solmv commented Apr 29, 2024

thank you!
I was having a hard time with the second part of the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment