def addToInventory(inventory, addedItems):
for k in addedItems:
inventory.setdefault(k, 0)
inventory[k] = inventory[k]+1
return inventory
def displayInventory(inventory):
print('Inventory:')
item_count = 0
for k, v in inventory.items():
print(k + ' -- ' + str(v))
item_count += v
print('Total number of items: ' + str(item_count))
inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'gold coin', 'ruby']
inv = addToInventory(inv, dragonLoot)
displayInventory(inv)
5.6.2 列表到字典
最新推荐文章于 2024-05-31 14:22:35 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)