1#!/usr/bin/env python3.52#Coding:utf-83#5.6.14#List of items for fun games5#given a dictionary that contains the item name and quantity, and prints out the quantity corresponding to the item6 7 Dict_stuff = {'Rope': 1,'Torch': 6,'Gold coin': 42,'Dagger': 1,'Arrow': 12} 8Print("5.6.1 Reference Answers") 9Print('='* 80) 10Print("Given dictionary:", Dict_stuff)11Print("Operation Result:") 12defDisplayinventory (Inventory):13Print("Inventory:") Item_total =015 forKvinchInventory.items ():16Print(Str (v) +'\ t'+k)Item_total + =v18Print("Total number of items:"+str (item_total))19displayinventory (Dict_stuff)20Print('='* 80) 21stPrint() 22#5.6.2Dragonloot = ['Gold coin','Dagger','Gold coin','Gold coin','Ruby'] 24Print("5.6.2 Reference Answers") 25Print('='* 80) INV = {'Gold coin': 42,'Rope': 1} 27Print("given list:", Dragonloot)28Print("Given dictionary:", INV)29Print("Operation Result:") 30defaddtoinventory (inventory,addeditems):31 forIteminchDragonloot:32ifItem not inchInventory.keys ():33Inventory.setdefault (Item,addeditems.count (item))34Else: + Inventory[item] + = 1 36returnInventoryNotoginseng INV =addtoinventory (inv,dragonloot)38Print(INV)39displayinventory (INV)40Print('='* 80)
Python Programming Quick Start 5th Chapter Practical Project Reference Answer