Python程序设计(第三版)约翰·策勒 编程练习课后答案(第二章)
2.1?修改convert.py程序,打印介紹。
#File: 2.1.py #-*- coding: utf-8 -*- #修改convert.py程序打印介紹。def convert():'溫度轉(zhuǎn)換' #函數(shù)說明print("This program converts the temperature from Celsius into Fahrenheit.")#打印程序說明print("-------------------------------------------------------------------")celsius = eval(input("What's the Celsius temperature?\n"))fahernheit = 9/5 * celsius + 32print("The tempature is", fahernheit, "degrees Fahrenheit")convert()方法一:在主函數(shù)中使用print函數(shù)在程序執(zhí)行前打印程序介紹(主函數(shù)第二行)。
方法二:在主函數(shù)的第一行添加函數(shù)的說明文檔。
? ? ? ? ? ? ? 出現(xiàn)在函數(shù)名下方的字符串就是函數(shù)的說明文檔。使用說明文檔的好處是可以使用help()查看,效果如下:
help()函數(shù)是干什么的呢?在Python IDLE中鍵入help(help)可以查看它的功能。(邏輯似乎有些混亂  ̄へ ̄!)
? 說明文檔是這樣寫的:
??
所以,與Linux中的系統(tǒng)幫助命令man類似,Python中的help()用于查看括號內(nèi)對象的幫助文檔。
2.2、在程序結(jié)束時(shí)添加語句,時(shí)程序暫停
在裝有Python的系統(tǒng)上,雙擊.py文件,程序完成后會立即退出。如果在2.1函數(shù)末尾添加input("press the <Enter> key to quit."),函數(shù)執(zhí)行到打印溫度后會等待,直到鍵盤鍵入回車,程序才會結(jié)束。
這實(shí)際上是利用了Python內(nèi)置函數(shù)input的特性,當(dāng)input函數(shù)被調(diào)用時(shí),會在屏幕上打印提示并等待用戶鍵入文本,直到用戶鍵入<Enter>鍵,input函數(shù)才會結(jié)束。
2.3、修改avg2.py,找出三個(gè)考試成績的平均值。
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: ")) #將輸入改為三個(gè)average = (s1 + s2 + s3)/3print("The average score is: ", average)main()運(yùn)行結(jié)果:
2.4、使用循環(huán)修改convert.py,讓它在推退出前執(zhí)行5次,每次通過循環(huán)。程序應(yīng)該從用戶獲得另一個(gè)溫度,并打印轉(zhuǎn)換的值。
#File: 2.4.py #-*- coding: utf-8 -*- #修改convert.py程序,使用循環(huán)轉(zhuǎn)換5次溫度def convert_5():'5次溫度轉(zhuǎn)換' #函數(shù)說明print("This program converts the temperature from Celsius into Fahrenheit.")#打印程序說明print("-------------------------------------------------------------------")for i in range(5): #使用for循環(huán)將轉(zhuǎn)換語句執(zhí)行5次celsius = eval(input("What's the Celsius temperature?\n"))fahernheit = 9/5 * celsius + 32print("The tempature is", fahernheit, "degrees fahrenheit")input("Press enter to quit.")convert_5()運(yùn)行結(jié)果:
2.5、修改convert.py程序,讓它計(jì)算并打印一個(gè)攝氏度和華氏度的對應(yīng)表,從0℃到100℃,每隔10 ℃一個(gè)值
可以通過安裝prettytable模塊輸出表格,這里偷個(gè)懶,用格式化輸出調(diào)整輸出數(shù)值的長度和距離,產(chǎn)生一個(gè)簡易的輸出表。
#File: 2.5.py #-*- coding: utf-8 -*- #修改convert.py程序,打印攝氏度和華氏度的對應(yīng)表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循環(huán)將轉(zhuǎn)換語句執(zhí)行11次fahernheit = 9/5 * celsius + 32print("%-7.2f\t%-7.2f"%(celsius, fahernheit)) #輸出的溫度值長度為7,保留兩位小數(shù)# input("Press enter to quit.")printTable()輸出結(jié)果:
2.6、修改futval.py程序,讓投資的年數(shù)也由用戶輸入。確保修改最后的消息,以反映正確的年數(shù)。
# 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: ")) #輸入投資年數(shù)for i in range(year): #循環(huán)次數(shù)與年數(shù)一致principal = principal * (1 + apr)print("The value in %d years is:"%year, principal) #輸出的年數(shù)與輸入一致main()2.7、假設(shè)你有一個(gè)投資計(jì)劃,每年投資一定固定金額。修改futval.py程序,計(jì)算你的投資總累積值。該程序的輸入將是每年投資的金額,利率和投資的年數(shù)。
程序 終值
輸入
? ? principal 每年投資于美元的金額
? ? apr 以十進(jìn)制表示的每年的年度百分比利率
輸出 投資n年后的終值
關(guān)系 第n年后的價(jià)值為(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都是元組,其中元素不能被修改,所以需要將類型轉(zhuǎn)換為列表principal, apr = list(principal), list(apr)year = len(principal)for i in range(year): #循環(huán)次數(shù)與年數(shù)一致principal[i] = (principal[i-1] + principal[i]) * (1 + apr[i])print("The value in %d years is:"%year, principal[-1]) #輸出的年數(shù)與輸入一致main()?
總結(jié)
以上是生活随笔為你收集整理的Python程序设计(第三版)约翰·策勒 编程练习课后答案(第二章)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工资待遇情况的分析研究
- 下一篇: 笔记本电脑用u盘一键重装系统步骤