我有以下問題:如何更新列表項目的字典?
想象一下,我殺了一條龍並且它掉落了戰利品,我該如何從戰利品中更新我的庫存?我想如何追加如果戰利品不存在庫存,但如果他們已經在那裏,我不知道如何更新它。
這裏是代碼:
UserInventory = {'rope': 1, 'torch':6, 'gold coin':42, 'dagger': 1, 'arrow': 12}
def showstuff(storeno):
items_total = 0
for k, v in storeno.items():
print('Item :' + k + '---' + str(v))
items_total = items_total + v
print('Total Items:' + str(items_total))
'''def addstuff(inventory, additem):
I'm not sure what to do here
dragonloot = ['gold coin', 'gold coin', 'rope']
addstuff(UserInventory, dragonloot)'''
showstuff(UserInventory)
無需創建3個計數器。 'inventory_ctr.update(['rope','torch'])'會做。 –
非常感謝!現在我知道一個新的Python模塊! – Kinghin245