Python programming Quick Start Chapter 1 practical project reference answers, python Quick Start

Source: Internet
Author: User

Python programming Quick Start Chapter 1 practical project reference answers, python Quick Start

1 #! /Usr/bin/env python3.5 2 # coding: UTF-8 3 #5.6.1 4 # Fun game Item List 5 # A dictionary containing the item name and quantity is given, and print the number of items corresponding to 6 7 dict_stuff = {'rope': 1, 'torch': 6, 'Gold coin ': 42, 'dagger': 1, 'arrow': 12} 8 print ("5.6.1 reference answer") 9 print ('=' * 80) 10 print ("given dictionary:", dict_stuff) 11 print ("Run Result:") 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 = ['goldcoin ', 'dagger', 'goldcoin ', 'Gold coin ', 'Ruby'] 24 print ("5.6.2 reference answer") 25 print ('=' * 80) 26 inv = {'gold coin ': 42, 'rope': 1} 27 print ("given list:", dragonLoot) 28 print ("given dictionary:", inv) 29 print ("running result :") 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)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.