Python程序设计(第三版)约翰·策勒 编程练习课后答案(第二章)

2.1 修改convert.py程序,打印介绍。

#File: 2.1.py 
#-*- coding: utf-8 -*-
#修改convert.py程序打印介绍。

def convert():
    '温度转换'                    #函数说明
    print("This program converts the temperature from Celsius into Fahrenheit.")
    #打印程序说明
    print("-------------------------------------------------------------------")
    celsius = eval(input("What's the Celsius temperature?\n"))
    fahernheit = 9/5 * celsius + 32
    print("The tempature is", fahernheit, "degrees Fahrenheit")
    
    
convert()

方法一:在主函数中使用print函数在程序执行前打印程序介绍(主函数第二行)。

方法二:在主函数的第一行添加函数的说明文档。

              出现在函数名下方的字符串就是函数的说明文档。使用说明文档的好处是可以使用help()查看,效果如下:


help()函数是干什么的呢?在Python IDLE中键入help(help)可以查看它的功能。(逻辑似乎有些混乱  ̄へ ̄!)

  说明文档是这样写的:

  

所以,与Linux中的系统帮助命令man类似,Python中的help()用于查看括号内对象的帮助文档。


2.2、在程序结束时添加语句,时程序暂停

在装有Python的系统上,双击.py文件,程序完成后会立即退出。如果在2.1函数末尾添加input("press the <Enter> key to quit."),函数执行到打印温度后会等待,直到键盘键入回车,程序才会结束。

这实际上是利用了Python内置函数input的特性,当input函数被调用时,会在屏幕上打印提示并等待用户键入文本,直到用户键入<Enter>键,input函数才会结束。

2.3、修改avg2.py,找出三个考试成绩的平均值。

def main():
    print("This program computes the average of three exam scores.")
    s1, s2, s3 = eval(input("Enter the three scores separated by a comma: ")) #将输入改为三个
    average = (s1 + s2 + s3)/3
    print("The average score is: ", average)

main()

运行结果:

2.4、使用循环修改convert.py,让它在推退出前执行5次,每次通过循环。程序应该从用户获得另一个温度,并打印转换的值。

#File: 2.4.py 
#-*- coding: utf-8 -*-
#修改convert.py程序,使用循环转换5次温度

def convert_5():
    '5次温度转换'                    #函数说明
    print("This program converts the temperature from Celsius into Fahrenheit.")
    #打印程序说明
    print("-------------------------------------------------------------------")
    for i in range(5):                                                #使用for循环将转换语句执行5次
        celsius = eval(input("What's the Celsius temperature?\n"))
        fahernheit = 9/5 * celsius + 32
        print("The tempature is", fahernheit, "degrees fahrenheit")
    input("Press enter to quit.")
    
convert_5()

运行结果:

2.5、修改convert.py程序,让它计算并打印一个摄氏度和华氏度的对应表,从0℃到100℃,每隔10 ℃一个值

可以通过安装prettytable模块输出表格,这里偷个懒,用格式化输出调整输出数值的长度和距离,产生一个简易的输出表。

#File: 2.5.py 
#-*- coding: utf-8 -*-
#修改convert.py程序,打印摄氏度和华氏度的对应表

def printTable():
    print("This program prints the convertation printTable of Fahrenheit and Celsius.")
    print("-------------------------------------------------------------------\n")
    print("Celsius\tFahrenheit\n","-"*20)
    for celsius in range(0, 110, 10):             #使用for循环将转换语句执行11次
        fahernheit = 9/5 * celsius + 32
        print("%-7.2f\t%-7.2f"%(celsius, fahernheit))   #输出的温度值长度为7,保留两位小数
   
   # input("Press enter to quit.")
    
printTable()

输出结果:

2.6、修改futval.py程序,让投资的年数也由用户输入。确保修改最后的消息,以反映正确的年数。

# File:2.6.py
# futval
# -*- coding:utf-8 -*-

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

    principal = eval(input("Enter the initial principal: "))
    apr = eval(input("Enter the annual interest rate: "))
    year = eval(input("Enter the year of investment: ")) #输入投资年数

    for i in range(year):                                #循环次数与年数一致
        principal = principal * (1 + apr)
    print("The value in %d years is:"%year, principal)   #输出的年数与输入一致

main()
    

2.7、假设你有一个投资计划,每年投资一定固定金额。修改futval.py程序,计算你的投资总累积值。该程序的输入将是每年投资的金额,利率和投资的年数。

程序 终值

输入

    principal 每年投资于美元的金额

    apr 以十进制表示的每年的年度百分比利率

输出 投资n年后的终值

关系 第n年后的价值为(principal+principal(n - 1))*(1 + apr( n ))

# File:2.7.py
# futval
# -*- coding:utf-8 -*-

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

    principal = eval(input("Enter the investment of every year: "))
    apr = eval(input("Enter the annual interest rate of each year: "))
    #principal和apr都是元组,其中元素不能被修改,所以需要将类型转换为列表
    principal, apr = list(principal), list(apr)
    year = len(principal)
    
    for i in range(year):                                               #循环次数与年数一致
        principal[i] = (principal[i-1] + principal[i]) * (1 + apr[i])
    print("The value in %d years is:"%year, principal[-1])              #输出的年数与输入一致

main()
    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值