第一题
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 mathfrom 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程序设计第三版约翰策勒第六章编程练习答案》对你有帮助,请点赞、收藏,并留下你的观点哦!