1. 程式人生 > >Python程式設計快速上手——讓繁瑣工作自動化第五章實踐題

Python程式設計快速上手——讓繁瑣工作自動化第五章實踐題

##5.6.1
參照書中提示答案

##5.6.2

def hanshu1(canshu1, canshu2):
    for a in canshu2:
        ziliao[a] = (canshu1.get(a, 0) + 1)
    return ziliao

def hanshu2(canshu):
    print("Inventory: ")
    zongliang = 0
    for k, v in ziliao.items():
        print(str(v) + ' ' + k)
        zongliang += v
    print
() print('Total number of items: ' + str(zongliang)) ziliao = {'gold coin': 42, 'rope': 1} tulong = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby'] ziliao = hanshu1(ziliao, tulong) hanshu2(ziliao)