python编程快速上手之第5章实践项目参考答案

 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)
时间: 2024-08-05 11:17:35

python编程快速上手之第5章实践项目参考答案的相关文章

python编程快速上手之第10章实践项目参考答案

  本章主要讲了python程序的调试,当程序有BUG或异常的时候,我们如何调试代码找出问题点.其实在本章之前的章节我们做练习的时候都会遇到各种各样的错语和异常,最初当不知道程序哪里出错的情况下不可否认的都使用了print语句进行输出并调试代码.没错print也是调试代码的一种工具,直观简单,便也有缺点,就是调试好后要进行删除,也是件麻烦事,于是就有了本章介绍的assert(断言),logging(日志)以及各种调试工具的出现. 首先来回顾一下python的异常. 一.python常见的异常类型

python编程快速上手之第8章实践项目参考答案

第8章实践项目之疯狂填词 创建一个一个疯狂填词(Mad Libs),程序,它将读入文本文件,并让用户在该文本文件中出现 ADJECTIVE,NOUN,VERB等单词的地方,加上他们自己的文本. 首先准备一个a.txt的文本文件 程序代码如下: #!/usr/bin/env python3.4 # coding:utf-8 # 8.9.2 import re f1 = open('a.txt','r') strf1 = f1.read() print("原文件内容为:") print(s

python编程快速上手之第10章实践项目参考答案(11.11.2)

#!/usr/bin/env python # -*- coding:utf-8 -*- import os import re import urllib import json import socket import urllib.request import urllib.parse import urllib.error # 设置超时 import time timeout = 5 socket.setdefaulttimeout(timeout) class Crawler: # 睡

python编程快速上手之第3章实践项目参考答案

1 #!/usr/bin/env python 2 # coding:utf-8 3 # write by mfyang 4 # collatz.py 5 # 从用户读入一个值,并判断这个值是不是一个int类型的整数,如果不是给出异常提示 6 # 如果这个值是偶数 那么让这个数//2 7 # 如果这个值是奇数 那么让这个数 ×3 + 1 8 # 通过不断调用函数的返回值 并打印这个返回值 直到这个返回值为1 9 10 num = raw_input("please input a number:

python编程快速上手之第18章实践项目参考答案(18.4.1)

import pyautogui, time print('Press Ctrl-C to quit.') try: while True: time.sleep(10) pyautogui.moveRel(1, 0, duration=0.25) time.sleep(10) pyautogui.moveRel(-1, 0, duration=0.25) except KeyboardInterrupt: print('\nDone.')

python编程快速上手之第15章实践项目参考答案(17.7.1)

#! python3 # resizeAndAddLogo.py - Resizes all images in current working directory to fit # in a 300x300 square, and adds catlogo.png to the lower-right corner. import os from PIL import Image os.chdir('C:\\Users\\Administrator\\Python35-32\\test\\ku

python编程快速上手之第13章实践项目参考答案(13.6.1)

import os,PyPDF2 os.chdir('D:\\My Documents') for folderName, subfolders, filenames in os.walk('D:\\My Documents'): for file in filenames: if file.endswith('.pdf'): pdfFile = open(file, 'rb') pdfReader = PyPDF2.PdfFileReader(pdfFile) pdfWriter = PyPD

python编程快速上手之第15章实践项目参考答案(17.7.2)

#! python3 # Import modules and write comments to describe this program. import zipfile, os from PIL import Image from PIL import ImageFile #os.chdir('D:\\My Documents\\') ImageFile.LOAD_TRUNCATED_IMAGES = True for foldername, subfolders, filenames i

python编程快速上手之第12章实践项目参考答案(12.13.3)

1 #! python3 2 import openpyxl,os,glob 3 os.chdir('C:\\Users\\Administrator\\Python35-32') 4 list=glob.glob('*.txt') 5 print(list) 6 wb = openpyxl.Workbook() 7 sheet = wb.get_active_sheet() 8 columnNum=0 9 for fliename in list: 10 file_object = open(

python编程快速上手之第10章实践项目参考答案(11.11.3)

from selenium import webdriver from selenium.webdriver.common.keys import Keys import time browser = webdriver.Firefox() url = 'https://gabrielecirulli.github.io/2048/' browser.get(url) game_ele = browser.find_element_by_class_name('game-container')