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

第一题

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 = 0
    for i in range(n+1):
        sum = sum + i
    return sum

def sumNCubes(n):
    sum = 0
    for i in range(n+1):
        sum = sum + i**3
    return sum

def 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 d

def Area(a, b, c):
    p = (a + b + c) / 2
    s = math.sqrt(p * (p - a) * (p - b) * (p - c))
    return s

def 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 math

def main():
    n = int(input("Enter the times you want to calculate: "))
    x = float(input("Enter the figure: "))
    guess = x/2
    for 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)/2

main()

第九题

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])**2
    print(*nums)


main()

第十二题

def main():
    nums = input("Enter the numbers: ").split(",")
    sumList(nums)

def sumList(nums):
    sum = 0
    for 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 nums


main()

第十四题

def toNumbers(strList):
    for i in range(len(strList)):
        strList[i] = float(strList[i])

def sumList(nums):
    total = 0
    for n in nums:
        total = total + n
    return total

def squareEach(nums):
    for i in range(len(nums)):
        nums[i] = nums[i]**2

def 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 * size
    eyeOff = size / 3.0
    mouthSize = 0.8 * size
    mouthOff = size / 2.0
    head = 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 program


from graphics import *

def drawFace(center, size, window):
    eyeSize = 0.15 * size
    eyeOff = size / 3.0
    mouthSize = 0.8 * size
    mouthOff = size / 2.0
    head = 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)**.5

def 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 win
    
def 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()

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值