5.6.2 列表到字典的函数,针对好玩游戏物品清单
#automate 5.6.2
def displayInventory(inventory):
print('Inventory:')
itemTotal = 0
for x,y in inventory.items():
print(str(y) + ' ' + x)
itemTotal += y
print()
print('Total number of items: ' + str(itemTotal))
def addToInventory(inventory,addedItems):
for item in addedItems:
inventory[item] = inventory.get(item,0) + 1
return inventory
dragonLoot = ['gold coin', 'dagger', 'gold coin' , 'gold coin', 'ruby']
stuff = {'rope':1,'gold coin':42}
stuff = addToInventory(stuff,dragonLoot)
displayInventory(stuff)
纯属个人笔记:)