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)

你可能感兴趣的:(Python编程,Python编程快速上手)