第一题

def lrc(animal, call):m()print("And on his farm he had a {0}, Ee I Ee I Oh!".format(animal))print("With a {0}, {0}here, and a {0}, {0} there.".format(call))print("Here a {0}, there a {0}, everywhere a {0}, {0}.".format(call))m()print()def m():print("Old Macdonald had a farm, Ee I Ee I Oh!")def main():ANIMAL = ["cat", "dog", "duck", "cow", "pig"]CALL = ["meow", "woof", "quack", "moo", "oink"]for i in range(5):lrc(ANIMAL[i], CALL[i])main()

第二题

def main():NUM = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]BHV = ["suck his thumb", "tie his shoe", "climb a tree", "shut the door","take a dive", "pick up sticks", "pray to heaven", "shut the gate","check the time", "say 'THE END'"]for i in range(10):march(NUM[i], BHV[i])def march(num, bhv):print("The ants go marching {0} by {0}, hurrah, hurrah".format(num))print("The ants go marching {0} by {0}, hurrah, hurrah".format(num))print("The ants go marching {0} by {0},".format(num))print("The little one stops to {0}".format(bhv))print("And they all go marching down to the ground")print("To get out of the rain, BOOM! BOOM! BOOM!\n")main()

第三题 比较重复,略

第四题

def sumN(n):sum = 0for i in range(n+1):sum = sum + ireturn sumdef sumNCubes(n):sum = 0for i in range(n+1):sum = sum + i**3return sumdef main():n = int(input("Enter n: "))print("The sum of n and its cube are {0} and {1}".format(sumN(n),sumNCubes(n)))main()

第五题 重复,略

第六题

import math
from graphics import *def dist(p1, p2):d = math.sqrt((p2.getX() - p1.getX()) ** 2 + (p2.getY() - p1.getY()) ** 2)return ddef Area(a, b, c):p = (a + b + c) / 2s = math.sqrt(p * (p - a) * (p - b) * (p - c))return sdef main():win = GraphWin("Triangle", 800, 800)win.setCoords(0, 0, 25, 25)win.setBackground(color_rgb(253, 253, 253))message = Text(Point(12.5, 24), "Click three points to get a triangle")message.draw(win)p1 = win.getMouse()p1.draw(win)p2 = win.getMouse()p2.draw(win)p3 = win.getMouse()p3.draw(win)triangle = Polygon(p1, p2, p3)triangle.setWidth(2)triangle.setOutline("black")triangle.setFill(color_rgb(144, 144, 144))triangle.draw(win)area = Area(dist(p1, p2), dist(p1, p3), dist(p2, p3))perim = dist(p1, p2) + dist(p1, p3) + dist(p2, p3)message.setText("The perimeter and the area of the triangle are {0:0.3f} and {1:0.3f}.".format(perim, area))win.getMouse()win.close()main()

第七题

def fib(n):P = [0,1,1]for i in range(n-1):P[2] = P[0] + P[1]P[0] = P[1]P[1] = P[2]return P[2]def main():n = int(input("Enter n: "))print("Fib:",fib(n))main()

第八题

import mathdef main():n = int(input("Enter the times you want to calculate: "))x = float(input("Enter the figure: "))guess = x/2for i in range(n):guess = nextGuess(guess,x)print("The possible is {0:0.4f},the real root is {1:0.4f},the error is {2:0.4f}.".format(guess,math.sqrt(x),abs(guess-math.sqrt(x))))def nextGuess(guess,x):return (guess + x/guess)/2main()

第九题

def grade(score):d = int(score//10)grade = 'FFFFFFDCBAA'print("The grade level is",grade[d])def main():score = float(input("Enter the mark: "))grade(score)main()

第十题

def acronym(phrase):c = phrase.upper()d = c.split()for ch in d:print(ch[0],end = "")def main():phrase = input("Enter the word: ")acronym(phrase)main()

第十一题

def main():nums = input("Enter the number list: ").split(",")nums = squareEach(nums)def squareEach(nums):for i in range(len(nums)):nums[i] = str(int(nums[i]) ** 2)# nums[i] = int(nums[i])**2print(*nums)main()

第十二题

def main():nums = input("Enter the numbers: ").split(",")sumList(nums)def sumList(nums):sum = 0for ch in nums:sum = sum + float(ch)print(sum)main()

第十三题

def main():nums = input("Enter the strings: ").split(",")nums = toNumbers(nums)print("Is nums[i] a number?")print(type(nums[0]) is float)def toNumbers(nums):nums = [float(x) for x in nums]return numsmain()

第十四题

def toNumbers(strList):for i in range(len(strList)):strList[i] = float(strList[i])def sumList(nums):total = 0for n in nums:total = total + nreturn totaldef squareEach(nums):for i in range(len(nums)):nums[i] = nums[i]**2def main():print("Program to find sum of squares from numbers in a file")fname = input("Enter filename: ")data = open(fname,'r').readlines()toNumbers(data)squareEach(data)print("Sum of squares:", sumList(data))main()

第十五题

def drawFace(center, size, window):eyeSize = 0.15 * sizeeyeOff = size / 3.0mouthSize = 0.8 * sizemouthOff = size / 2.0head = Circle(center, size)head.setFill("yellow")head.draw(window)leftEye = Circle(center, eyeSize)leftEye.move(-eyeOff, -eyeOff)rightEye = Circle(center, eyeSize)rightEye.move(eyeOff, -eyeOff)leftEye.draw(window)rightEye.draw(window)p1 = center.clone()p1.move(-mouthSize / 2, mouthOff)p2 = center.clone()p2.move(mouthSize / 2, mouthOff)mouth = Line(p1, p2)mouth.draw(window)def test():win = GraphWin("Faces")drawFace(Point(50, 50), 10, win)drawFace(Point(100, 100), 20, win)drawFace(Point(150, 150), 30, win)win.getMouse()win.close()test()

第十六题

# c06ex16.py
#   face drawing programfrom graphics import *def drawFace(center, size, window):eyeSize = 0.15 * sizeeyeOff = size / 3.0mouthSize = 0.8 * sizemouthOff = size / 2.0head = Circle(center, size)head.setFill("yellow")head.draw(window)leftEye = Circle(center, eyeSize)leftEye.move(-eyeOff, -eyeOff)rightEye = Circle(center, eyeSize)rightEye.move(eyeOff, -eyeOff)leftEye.draw(window)rightEye.draw(window)p1 = center.clone()p1.move(-mouthSize/2, mouthOff)p2 = center.clone()p2.move(mouthSize/2, mouthOff)mouth = Line(p1,p2)mouth.draw(window)def interactiveFace(w):center = w.getMouse()edge = w.getMouse()radius = distance(center, edge)drawFace(center, radius, w)def distance(p1, p2):dx = p2.getX() - p1.getX()dy = p2.getY() - p1.getY()return (dx*dx + dy*dy)**.5def createPicWin(picFile):img = Image(Point(0,0),picFile)width = img.getWidth()height = img.getHeight()win = GraphWin(picFile, width, height)img.move(width//2, height//2)img.draw(win)return windef main():print("Photo Anonymizer: Draw faces over pictures.")picFile = input("Enter name of file containing GIF image: ")win = createPicWin(picFile)numFaces = int(input("How many faces to draw? "))for i in range(numFaces):print("Click center and edge of a face.")interactiveFace(win)print("Click again to quit.")win.getMouse()win.close()main()

十七题

from graphics import *def moveTo(shape,newCenter):center = shape.getCenter()dx = newCenter.getX() - center.getX()dy = newCenter.getY() - center.getY()shape.move(dx,dy)
def main():win = GraphWin("move circle for 10 times",800,800)win.setBackground("white")win.setCoords(-40,-40,40,40)cic = Circle(Point(0,0),5)cic.setFill("yellow")cic.setOutline("black")cic.setWidth(2)cic.draw(win)for i in range(10):p = win.getMouse()x,y = p.getX(),p.getY()moveTo(cic,p)print("Click to quit.")win.getMouse()win.close()
main()

python程序设计第三版约翰策勒第六章编程练习答案相关推荐

  1. C Primer Plus(第六版)第十六章 编程练习答案

    距离上次隔了十二天,好像是有点慢,期间还看了下C++pp,看到句话,每章内容不是很多,建议一周内完成一章,虽然之后要看琢石成器,C++也要顺带看一下.--11.16 CH16 Code answer ...

  2. python程序设计 第三版 董付国_Python程序设计(第3版)

    章 基础知识 1.1 如何选择Python版本 1.2 Python安装与简单使用 1.3 使用pip管理Python扩展库 1.4 Python基础知识 1.4.1 Python对象模型 1.4.2 ...

  3. python程序设计第三版课后答案第六章_python程序设计 第六章答案

    参考答案如下 程序[名词解释] 囊式封堵 设计[单选] 变电所的主要功能是(). [判断题] 单独一根仪表管的支架间距应不大于700mm,章答同一管道支架间距应相等. [单选] 在电路计算时如果不作说 ...

  4. C语言程序设计教程(第三版)李凤霞 第一章课后习题答案

    第一章:程序设计基础知识 一. 单项选择题 1.面向过程的程序设计语言是________. A)机器语言 B)汇编语言 C)高级语言 D)第四代语言 2.程序设计一般包含以下四个步骤,其中其中首先应该 ...

  5. Java面向对象程序设计(第二版)袁绍欣 第九章课后习题答案

    线程和进程的联系和区别是什么? 区别: 进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动,它是系统进行资源分配和调度的一个独立单位. 线程是进程的一个实体,是CPU调度和分配的基本单位. ...

  6. python程序设计第3版课后答案_python程序设计第三版课后答案

    python程序设计第三版课后答案 更多相关问题 该图中XOY为地轴,MN为赤道,EF.E′F′为回归线,ST.S′T′为极圈.读图,回答下列问题.(10分)(1)目前黄赤交角在图上 (共10分,每空 ...

  7. 琢石成器――windows环境下32位汇编语言程序设计(第三版)笔记

    琢石成器――windows环境下32位汇编语言程序设计(第三版)笔记 2011年12月20日 基础篇 第1章 背景知识 1 1.1 Win32的软硬件平台 1.1.1 80x86系列处理器简史 1.1 ...

  8. Js高级程序设计第三版学习(十二章)

                                  Js高级程序设计第三版学习(十二章) 第十二章 DOM2和DOM3   1.样式: 访问样式属性 任何支持style特性的HTML元素都有一 ...

  9. JavaScript高级程序设计第三版.CHM【带实例】

    从驱动全球商业.贸易及管理领域不计其数的复杂应用程序的角度来看,说 JavaScript 已经成为当今世界上最流行的编程语言一点儿都不为过. JavaScript 是一种非常松散的面向对象语言,也是 ...

最新文章

  1. SpringBoot-web开发(四): SpringMVC的拓展、接管(源码分析)
  2. ANDROID 中设计模式的採用--创建型模式
  3. 网站权重增长缓慢,优化技巧是关键!
  4. Glide核心设计一:皮皮虾,我们走
  5. sql 找到最近的值_数据分析——SQL查询(常用函数)
  6. python编写脚本,删除固定用户下的所有表
  7. Codeforces #471
  8. Linux学习笔记10
  9. cudaMemset的调用方式
  10. 邮件协议POP3/IMAP/SMTP服务的区别
  11. Linux理论05:Linux的目录结构
  12. 伯克利区块链课程:替代密码验证,比特币应用数字签名的进化之路
  13. pandas入门学习
  14. 扫雷游戏网页版_佛性扫雷 炸不炸随缘
  15. springboot校园二手交易平台的设计与实现毕业设计源码260839
  16. PAT题集2019.6.25排名变动
  17. 华为2019年3月27日实习生笔试题及解答
  18. 为什么计算机无法访问u盘,WIN10系统无法访问U盘怎么处理_win10电脑u盘无法访问拒绝访问如何解决-win7之家...
  19. 智能防盗报警系统前端报警设备的选择
  20. 视频编解码——视频编解码器工作原理

热门文章

  1. 地图导航APP开发功能
  2. rsa 长密钥实现及测试
  3. git push失败unable to access ‘https://github.com/...‘的解决办法
  4. Keil无法跳转到(go to definition)函数定义的地方,而是出现Browser
  5. [原创]WIN8系统的远程桌面漏洞 利用QQ拼音纯净版实现提权
  6. 使用WebRTC搭建前端视频聊天室-01——入门篇
  7. hostapd源码编译与配置
  8. 织梦教程:ms在channel标签中调用typeid无效解决办法
  9. Linux arm cpu topology
  10. 流动模型、物质导数与速度散度的物理意义