《Python編程快速上手—讓繁瑣工作自動(dòng)化》第5章實(shí)踐項(xiàng)目答案

5.6 好玩游戲物品清單

def displayInventory(dict_items):
    total_number = 0
    for key, value in dict_items.items():
        print(f'{value} {key}')
        total_number += value
    print(f'Total number of items: {total_number}')
    return


def addToInventory(dict_items, addeditems):
    for item in addeditems:
        dict_items.setdefault(item, 0)
        dict_items[item] += 1


stuff = {'rope': 1, 'torch': 6, 'dagger': 1, 'gold coin': 42, 'arrow': 12}
displayInventory(stuff)

dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
print(dragonLoot)
addToInventory(stuff, dragonLoot)
displayInventory(stuff)

程序運(yùn)行結(jié)果

1 rope
6 torch
1 dagger
42 gold coin
12 arrow
Total number of items: 62
['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
1 rope
6 torch
2 dagger
45 gold coin
12 arrow
1 ruby
Total number of items: 67

Process finished with exit code 0
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容