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 + 32print("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)/3print("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 + 32print("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 + 32print("%-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()?
總結
以上是生活随笔為你收集整理的Python程序设计(第三版)约翰·策勒 编程练习课后答案(第二章)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工资待遇情况的分析研究
- 下一篇: python实现情人节的爱意表达