下面是我正在测试的代码:
stuff = {'arrow':12, 'gold coin':42, 'rope':1, 'torch':6, 'dagger':1}
def displayInventory(inventory):
print('Inventory:')
item_total = 0
for k, v in inventory.items():
print(str(v) + ' ' + str(k))
item_total += v
print('Total number of items: ' + str(item_total))
displayInventory(stuff)
##
inv = {'gold coin':42, 'rope':1}
dragonLoot = {'gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby'}
def addToInventory(inventory, addedItems):
for i in addedItems:
inventory.setdefault(i, 0)
inventory[i] += 1
return inventory
dragonLoot = {'gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby'}
inv = addToInventory(inv, …Run Code Online (Sandbox Code Playgroud)