for循环遍历字典里所有键值对_python

stuff = {'绳子':1, '火把':6, '金币': 42, '短剑': 1, '箭':12}

def displayInventory(inventory):
    print('物品清单:')
    item_total = 0
    for k, v in inventory.items():
        print(str(v) + ' ' + k)
        item_total += v
    print("物品总数:" + str(item_total))

displayInventory(stuff)

  

猜你喜欢

转载自www.cnblogs.com/dogfaraway/p/12356598.html