首先,boss是一条龙,掉的装备如下
dragonLoot = ['gold coin','dagger','gold coin','gold coin','ruby']
你的背包里面物品如下
inv = {'gold coin':42,'rope':1}
打完boss之后背包更新
重新更改背包物品及数量
效果如下
代码如下:
def addToInventory(inventory , addedItems ): # 更新背包
print('背包更新中:')
c1 = {}
for something in addedItems: #将传到的表格转换为字典,元素作为键,次数作为值
c1.setdefault(something,0)
c1[something] +=1
for v in inventory.keys():
if v not in c1.keys():
c1.setdefault(v,0) #设置默认值,当这个元素存在,就用其本身的值,若不是,则用括号中所给的值
c1[v] += inventory[v]
for k in c1.keys():
if k in inventory:
c1[k] = c1[k] +inventory[k]
return c1
def displayInventory(inv): #打印字典,即打印背包的物品
for k ,v in inv.items():
print(str(v) +' '+ k )
inv = {'gold coin':42,'rope':1}
dragonLoot = ['gold coin','dagger','gold coin','gold coin','ruby']
print('背包物品:')
displayInventory(inv)
print('BOSS掉落物品:')
print(dragonLoot)
inv = addToInventory(inv,dragonLoot)
print('背包物品:')
displayInventory(inv)