python編程快速上手之第5章實踐項目參考答案

來源:互聯網
上載者:User

標籤:包含   turn   style   ruby   span   dagger   名稱   print   tde   

 1 #!/usr/bin/env python3.5  2 # coding:utf-8  3 # 5.6.1  4 # 好玩遊戲的物品清單  5 # 給定一個字典,包含物品名稱和數量,並列印出數量對應的物品  6   7 dict_stuff = {‘rope‘:1,‘torch‘:6,‘gold coin‘:42,‘dagger‘:1,‘arrow‘:12}  8 print("5.6.1參考答案")  9 print(‘=‘ * 80) 10 print("給定字典:",dict_stuff) 11 print("運行結果:") 12 def displayInventory(inventory): 13     print("Inventory:") 14     item_total = 0 15     for k,v in inventory.items(): 16         print(str(v) + ‘\t‘ + k) 17         item_total += v 18     print("Total number of items:" + str(item_total)) 19 displayInventory(dict_stuff) 20 print(‘=‘ * 80) 21 print() 22 # 5.6.2 23 dragonLoot = [‘gold coin‘,‘dagger‘,‘gold coin‘,‘gold coin‘,‘ruby‘] 24 print("5.6.2參考答案") 25 print(‘=‘ * 80) 26 inv = {‘gold coin‘:42,‘rope‘:1} 27 print("給定列表:",dragonLoot) 28 print("給定字典:",inv) 29 print("運行結果:") 30 def addToInventory(inventory,addedItems): 31     for item in dragonLoot: 32         if item not in inventory.keys(): 33             inventory.setdefault(item,addedItems.count(item)) 34         else: 35             inventory[item] += 1 36     return inventory 37 inv = addToInventory(inv,dragonLoot) 38 print(inv) 39 displayInventory(inv) 40 print(‘=‘ * 80)

 

python編程快速上手之第5章實踐項目參考答案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.