跳轉至內容

程式設計基礎/函式示例 Python

來自華夏公益教科書,開放的書籍,為開放的世界
 # This program asks the user for a Fahrenheit temperature, 
 # converts the given temperature to Celsius,
 # and displays the results.
 #
 # References:
 # https://www.mathsisfun.com/temperature-conversion.html
 # https://wikibook.tw/wiki/Python_Programming
 
 
 def get_fahrenheit():
     print("Enter Fahrenheit temperature:")
     fahrenheit = float(input())
     return fahrenheit
 
 
 def calculate_celsius(fahrenheit):
     celsius = (fahrenheit - 32) * 5 / 9
     return celsius
 
 
 def display_result(fahrenheit, celsius):
     print(str(fahrenheit) + "° Fahrenheit is " + 
         str(celsius) + "° Celsius")
 
 
 def main():
     fahrenheit = get_fahrenheit()
     celsius = calculate_celsius(fahrenheit)
     display_result(fahrenheit, celsius)
 
 
 main()
Enter Fahrenheit temperature:
 100
100.0° Fahrenheit is 37.77777777777778° Celsius

參考文獻

[編輯 | 編輯原始碼]
華夏公益教科書