#把 dL 中的物品添加到inv字典中。会导致 RuntimeError: dictionary changed size during iteration
def addToInventory(inventory, addedItems):
for i in range(len(addedItems)):
for k,v in inventory.items():# v = 42 1
if k == addedItems[i]:
v+=1
else:
inventory.setdefault(addedItems[i], 1)
inv = {'gold coin':42, 'rope':1}
dL = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
addToInventory(inv, dL)
print (inv)