Python程序设计(第三版) 课后题答案 第二章 [约翰·策勒 (著) 人民邮电出版社]

本文提供了关于Python编程中课程练习的解答,包括温度单位转换、投资计算、交互式计算器等内容,展示了实用技巧和用户体验优化。
摘要由CSDN通过智能技术生成

Python程序设计(第三版)课后题答案 第二章


编程练习

  • 1.一个用户友好的程序应该打印一个介绍,告诉用户程序用来做什么。修改convert.py程序,打印介绍。

答案:

# convert.py
#     A program to convert Celsius temps to Fahrenheit

def main():
    celsius = float(input("What is the Celsius temperature? "))

    fahrenheit = 9 / 5 * celsius + 32
    print("The temperature is", fahrenheit, "degrees Fahrenheit.")


main()
  • 2.在许多使用python的系统上,可以通过简单的点击(或双击)程序文件的图标来运行程序。如果你能够以这种方式运行convert.py程序,你就可能会发现另一个可用性问题。程序在新窗口中开始运行,但程序一完成,窗口就会消失,因此你无法读取结果。在程序结束时添加一个输入语句,让它暂停,给用户一个读取结果的机会。下面这样的代码应该有效:input("Press the <Enter> key to quit.")

答案:

def main():
    celsius = float(input("What is the Celsius temperature? "))

    fahrenheit = 9 / 5 * celsius + 32
    print("The temperature is", fahrenheit, "degrees Fahrenheit.")
    
    input("Press the <Enter> key to quit.")


main()

  • 3.修改avg2.py程序,找出三个考试成绩的平均值。

答案:

def main():
    print("This program computes the average of three exam scores.")

    score1, score2, score3 = eval(input("Enter three scores separated by a comma: "))
    average = (score1 + score2 + score3) / 3

    print("The average of the scores is:", average)


main()

输出:

This program computes the average of three exam scores.
Enter three scores separated by a comma: 70,85,98
The average of the scores is: 84.33333333333333
  • 4.使用循环修改convert.py程序,让它在退出前执行5次。每次通过循环,程序应该从用户获得另一个温度,并打印转换的值。

答案:

def main():
    for i in range(5):
        celsius = eval(input("What is the Celsius temperature? "))

        fahrenheit = 9 / 5 * celsius + 32
        print("The temperature is", fahrenheit, "degrees Fahrenheit.")

    input("Press the <Enter> key to quit.")


main()

输出:

What is the Celsius temperature? 10
The temperature is 50.0 degrees Fahrenheit.
What is the Celsius temperature? 20
The temperature is 68.0 degrees Fahrenheit.
What is the Celsius temperature? 30
The temperature is 86.0 degrees Fahrenheit.
What is the Celsius temperature? 40
The temperature is 104.0 degrees Fahrenheit.
What is the Celsius temperature? 50
The temperature is 122.0 degrees Fahrenheit.
Press the <Enter> key to quit.

进程已结束,退出代码0
  • 5.修改convert.py程序,让它计算并打印一个摄氏温度和华氏温度的对应表,从0℃到100℃,每隔10℃一个值。

答案:

def main():
    # celsius = eval(input("What is the Celsius temperature? "))
    celsius = 0
    for i in range(11):
        fahrenheit = 9 / 5 * celsius + 32
        print("celsius = ", celsius, "℃", "Fahrenheit =  ", fahrenheit, "℃.")
        celsius = celsius + 10

    input("Press the <Enter> key to quit.")


main()

输出:

celsius =  0 ℃ Fahrenheit =  32.0.
celsius =  10 ℃ Fahrenheit =  50.0.
celsius =  20 ℃ Fahrenheit =  68.0.
celsius =  30 ℃ Fahrenheit =  86.0.
celsius =  40 ℃ Fahrenheit =  104.0.
celsius =  50 ℃ Fahrenheit =  122.0.
celsius =  60 ℃ Fahrenheit =  140.0.
celsius =  70 ℃ Fahrenheit =  158.0.
celsius =  80 ℃ Fahrenheit =  176.0.
celsius =  90 ℃ Fahrenheit =  194.0.
celsius =  100 ℃ Fahrenheit =  212.0.
Press the <Enter> key to quit.

进程已结束,退出代码0
  • 6.修改futval.py程序,让投资的年数也由用户输入。确保更改最后的消息,以反映正确的年数。

答案:

def main():
    print("This program calculates the future value of a 10-year investment.")

    year = eval(input("Enter the number of years: "))
    principal = eval(input("Enter the initial principal: "))
    apr = eval(input("Enter the annual interest rate: "))

    for i in range(year):
        principal = principal * (1 + apr)

    print("The value in", year, "years is:", principal)


main()

输出:

This program calculates the future value of any year investment.
Enter the number of years: 12
Enter the initial principal: 1000
Enter the annual interest rate: 0.03
The value in 12 years is: 1425.7608868461791

进程已结束,退出代码0
  • 7.假设你有一个投资计划,每年投资一定的固定金额。修改futval.py,计算你的投资的总累计值。该程序的输入将是每年投资的金额、利率和投资的年数。

答案:

def main():
    print("This program calculates the future value of any year investment.")

    year = eval(input("Enter the number of years: "))
    principal = eval(input("Enter the initial principal: "))
    apr = eval(input("Enter the annual interest rate: "))
    investment = principal

    for i in range(year):
        principal = principal * (1 + apr)
        principal = principal + investment

    print("The value in", year, "years is:", principal - investment)


main()

输出:

This program calculates the future value of any year investment.
Enter the number of years: 10
Enter the initial principal: 1000
Enter the annual interest rate: 0.03
The value in 10 years is: 11807.795690814855

进程已结束,退出代码0
  • 8.作为APR的替代方案,账户所产生的利息通常通过名义利率复利数来描述。例如,如果利率为3%,利息按季度计算复利,则该账户实际上每三个月赚取0.75%的利息。请修改futval.py程序,用此方法输入利率。程序应提示用户每年的利率(rate)和利息每年复利的次数(periods)。要计算10年的价值,程序将循环10*periods次,并在每次迭代中累积rate/period的利息。

答案:

def main():
    print("This program calculates the future value of any year investment.")

    year = eval(input("Enter the number of years: "))
    principal = eval(input("Enter the initial principal: "))
    apr = 0.03 
    periods = 4  # 每季度计算复利一次,每年计算4次
    investment = principal
    for i in range(year):
        for j in range(periods):
            principal = principal * (1 + apr / 4)
        rate = (principal - investment) / investment
        rate = round(rate, 5)
        print("第", i+1, "年的利率为", rate, ";复利次数为4次")

    print("经过计算", year, "年后总的收益为:", round(principal, 5))


main()

输出:

This program calculates the future value of any year investment.
Enter the number of years: 10
Enter the initial principal: 1001 年的利率为 0.03034 ;复利次数为4次
第 2 年的利率为 0.0616 ;复利次数为4次
第 3 年的利率为 0.09381 ;复利次数为4次
第 4 年的利率为 0.12699 ;复利次数为4次
第 5 年的利率为 0.16118 ;复利次数为4次
第 6 年的利率为 0.19641 ;复利次数为4次
第 7 年的利率为 0.23271 ;复利次数为4次
第 8 年的利率为 0.27011 ;复利次数为4次
第 9 年的利率为 0.30865 ;复利次数为4次
第 10 年的利率为 0.34835 ;复利次数为4次
经过计算 10 年后总的收益为: 134.83486

进程已结束,退出代码0
  • 9.编写一个程序,将温度从华氏温度转换为摄氏温度。

答案:

def main():
    # 华氏温度--->摄氏温度
    fahrenheit = eval(input("What is the fahrenheit temperature? "))

    celsius = (fahrenheit - 32) * 5 / 9
    print("The Celsius temperature is:", celsius, "℃")

    input("Press the <Enter> key to quit.")


main()
  • 10.编写一个程序,将以千米为单位的距离转换为英里。1千米约为0.62英里。

答案:

def main():
    # 千米--->英里
    kilometers = eval(input("What is the kilometers ? "))

    mile = kilometers * 0.62
    print("The converted distance is:", mile, "英里")

    input("Press the <Enter> key to quit.")


main()
  • 11.编写一个程序以执行你自己选择的单位转换。确保程序打印介绍,解释它的作用。

答案:

#     A program that converts square kilometers into acres


def main():
    # 平方千米--->英亩
    kilometers = eval(input("What is the kilometers ? "))

    acres = kilometers * 247.1053815
    print("The converted area is:", acres, "英亩")

    input("Press the <Enter> key to quit.")


main()
  • 12.编写一个交互式Python计算器程序。程序应该允许用户键入数学表达式,然后打印表达式的值。加入循环,以便用户可以执行多次计算(例如,最多100个)。注意:要提前退出,用户可以通过键入一个错误的表达式,或简单的关闭计算器程序运行的窗口,让程序崩溃。

答案:

def main():
    for i in range(100):
        result = eval(input("请输入需要计算的表达式:"))
        print("计算结果是:", result)

    input("Press the <Enter> key to quit.")


main()
请输入需要计算的表达式:3+5
计算结果是: 8
请输入需要计算的表达式:3+6*8/4-6+5
计算结果是: 14.0
请输入需要计算的表达式:0/1
计算结果是: 0.0
请输入需要计算的表达式:1.2/0
Traceback (most recent call last):
  File "**************************", line 29, in <module>
    main()
  File "**************************", line 23, in main
    result = eval(input("请输入需要计算的表达式:"))
  File "<string>", line 1, in <module>
ZeroDivisionError: float division by zero

进程已结束,退出代码1



(如有错误,请读者留言指正!)
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值