《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 5 章 编程练习答案

1到4题比较简单,这里省略

5题6题代码

def main():
    prompt = input("Enter the name: ").lower().split()
    P = ""
    num = 0
    for ch in prompt:
        P = P + ch
    for p in P:
        num = num + ord(p) - 96

    print(num)


main()

7题8题答案 

def main():
    prompt = input("Enter the word: ").split()
    s = []
    key = int(input("Key: "))
    for p in prompt:
        q = []
        for i in range(len(p)):
            if ord(p[i]) + key <= ord("z"):
                q.append(chr(ord(p[i]) + key))
            else:
                q.append(chr(ord(p[i]) + key - 26))
            h = "".join(q)
        s.append(h)
    for ch in s:
        print(ch, end=" ")
main()

9题答案

def main():
    prompt=input("Enter the word: ").split()
    i = 0
    for p in prompt:
        i=i+1
    print("The number of the words is {0}.".format(i))
main()

10题答案

def main():
    A = input("Please enter the words: ").split()
    n = 0
    totalLength = 0
    for a in A:
        n = n + 1
        totalLength = totalLength+len(a)
    avg = j / i
    print("The average length is {0:0.3f}.".format(avg))
main()

 11题答案

def main():
    print("This program illlustrates a chaotic function")
    x1, x2 = eval(input("Enter two numbers between 0 and 1 seperated by a comma: "))
    n = int(input("Enter the change times: "))
    print("|{0:<7}|{1:<10}|{2:<9}|".format("index", x1, x2))
    print("_" * 30)
    for i in range(n):
        x1 = 3.9 * x1 * (1 - x1)
        x2 = 3.9 * x2 * (1 - x2)
        print("|{0:<7}|{1:<10.6f}|{2:<9.6f}|".format(i + 1, x1, x2))

main()

 12题答案

def main():
    principal = eval(input("Enter the initial principal: "))
    apr = float(input("Enter the annual interest rate: "))
    years = int(input("Enter the number of years: "))
    print()
    print("Year     Value")
    print("-------------------")
    for i in range(years + 1):
        print("{0:<8}${1:.2f}".format(i, principal))
        principal = principal * (1 + apr)

main()

13题,略

14题答案

def main():
    filename = input("Enter the filename: ")
    infile = open(filename, 'r')
    chars = infile.read()
    words = chars.split()
    lines = chars.split("\n")
    infile.close()

    print("Lines:", len(lines))
    print("Words:", len(words))
    print("Characters:", len(chars))


main()

15题答案

def mark(window, name, point, pos, m):
    a = 2
    Name = Text(Point(2, m+a - pos), name)
    Mark = Rectangle(Point(5, m +0.45 +a- pos), Point(5 + point / 10, m-0.45+a - pos))
    Mark.setWidth(2)
    Mark.setFill(color_rgb(144,144,255))
    Text(Point(18, m +a- pos), str(point)).draw(window)
    Name.draw(window)
    Mark.draw(window)


def main():
    filename = input("Enter the name of the file: ")
    infile = open(filename, "r", encoding="utf-8")
    data = infile.read()
    data1 = data.split("\n")
    m = int(data1[0])

    win = GraphWin("grades", 55*(m+2), 55*(m+2))
    win.setCoords(0, 0, 20, m+3)
    win.setBackground("white")

    for i in range(1,len(data1)):
        data2 = data1[i].split()
        name,point = data2[0],float(data2[1])
        mark(win, name, point, i, m)

    print("Click to quit.")
    infile.close()
    win.getMouse()
    win.close()

main()

16题答案

def main():
    filename = input("Enter the name of the file:")
    infile = open(filename, "r")
    inf = infile.read()
    win = GraphWin("Students' Grade", 300, 200)
    win.setCoords(0, 0, 18, 12)
    win.setBackground(color_rgb(10, 155, 20))
    for i in range(11):
        p = str(i)
        n = inf.count(p)
        drb(win, p, n, i)
    win.getMouse()
    win.close()
    infile.close()

def drb(window, p, n, i):
    u = i*1.58
    Text(Point(u + 1,0.5),p).draw(window)
    Text(Point(u + 1,1.5 + n),str(n)).draw(window)
    bar = Rectangle(Point(u + 0.54,1),Point(u + 1.46,1 + n))
    bar.setFill(color_rgb(34, 68, 255))
    bar.setOutline("red")
    bar.draw(window)

main()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值