Latest web development tutorials

Python 攝氏溫度轉華氏溫度

Document 對象參考手冊 Python3實例

以下實例演示瞭如何將攝氏溫度轉華氏溫度:

# -*- coding: UTF-8 -*-

# Filename : test.py
# author by : www.w3big.com

# 用户输入摄氏温度

# 接收用户收入
celsius = float(input('输入摄氏温度: '))

# 计算华氏温度
fahrenheit = (celsius * 1.8) + 32
print('%0.1f 摄氏温度转为华氏温度为 %0.1f ' %(celsius,fahrenheit))

執行以上代碼輸出結果為:

输入摄氏温度: 38
38.0 摄氏温度转为华氏温度为 100.4 

以上實例中,攝氏溫度轉華氏溫度的公式為celsius * 1.8 = fahrenheit - 32。 所以得到以下式子:

celsius = (fahrenheit - 32) / 1.8

Document 對象參考手冊 Python3實例