如何使用 Python 將攝氏度轉換為華氏度?
在本文中,我們將向您展示如何使用 Python 將攝氏度轉換為華氏度。
攝氏度
攝氏度是一種溫度計量單位,也稱為百分度。它是國際單位制匯出單位,被世界上大多數國家使用。
它以瑞典天文學家安德斯·攝爾西烏斯的名字命名。
華氏度
華氏度是一種溫度刻度,以波蘭裔德國物理學家丹尼爾·伽布里爾·華倫海特的名字命名,它使用華氏度作為溫度單位。
要獲得攝氏度的華氏度等值,請乘以1.8並加上32 -
f=c*1.8+32
或者我們可以使用另一個公式 -
f=(c*9/5)+32
使用第一個公式 f=c*1.8+32 將攝氏度轉換為華氏度
演算法(步驟)
以下是執行所需任務需要遵循的演算法/步驟 -
建立一個變數來儲存輸入的攝氏度溫度。
使用數學公式 f=c*1.8+32 將輸入的攝氏度溫度轉換為華氏度溫度。
列印給定輸入攝氏度溫度的華氏度等值。
示例
以下程式使用公式 f=c*1.8+32 將給定的輸入攝氏度溫度轉換為華氏度溫度 -
# input celsius degree temperature celsius_temp = 45 # converting celsius degree temperature to Fahrenheit fahrenheit_temp =celsius_temp*1.8+32 # printing the Fahrenheit equivalent of the given input celsius degree print("The Fahrenheit equivalent of 45 celsius = ", fahrenheit_temp)
輸出
執行上述程式將生成以下輸出 -
The Fahrenheit equivalent of 45 celsius = 113.0
使用 f=(c*9/5)+32 將攝氏度轉換為華氏度
演算法(步驟)
以下是執行所需任務需要遵循的演算法/步驟 -
建立一個變數來儲存輸入的攝氏度溫度。
使用數學公式 f=(c*9/5)+32 將輸入的攝氏度溫度轉換為華氏度溫度。
列印給定輸入攝氏度溫度的華氏度等值。
示例
以下程式使用公式 f=(c*9/5)+32 將給定的輸入攝氏度溫度轉換為華氏度溫度 -
# input celsius degree temperature celsius_temp = 45 # converting celsius degree temperature to Fahrenheit fahrenheit_temp = (celsius_temp*9/5)+32 # printing the Fahrenheit equivalent of celsius print("The Fahrenheit equivalent of 45 celsius = ", fahrenheit_temp)
輸出
執行上述程式將生成以下輸出 -
The Fahrenheit equivalent of 45 celsius = 113.0
使用使用者定義函式將攝氏度轉換為華氏度
演算法(步驟)
以下是執行所需任務需要遵循的演算法/步驟 -
建立一個函式convertCelsiustoFahrenheit(),該函式將給定的攝氏度溫度轉換為華氏度溫度
使用數學公式f=(c*9/5)+32 將傳遞的攝氏度溫度轉換為華氏度溫度到函式。
返回傳遞的攝氏度溫度的華氏度溫度。
建立一個變數來儲存輸入的攝氏度溫度。
透過將輸入的攝氏度作為引數傳遞來呼叫 convertCelsiustoFahrenheit() 函式。
列印給定攝氏度溫度的華氏度等值
示例
以下程式使用使用者定義函式和公式 f=(c*9/5)+32 將給定的輸入攝氏度溫度轉換為華氏度溫度 -
# creating a function that converts the given celsius degree temperature # to Fahrenheit degree temperature def convertCelsiustoFahrenheit(c): # converting celsius degree temperature to Fahrenheit degree temperature f = (9/5)*c + 32 # returning Fahrenheit degree temperature of given celsius temperature return (f) # input celsius degree temperature celsius_temp = 80 print("The input Temperature in Celsius is ",celsius_temp) # calling convertCelsiustoFahrenheit() function by passing # the input celsius as an argument fahrenheit_temp = convertCelsiustoFahrenheit(celsius_temp) # printing the Fahrenheit equivalent of the given celsius degree temperature print("The Fahrenheit equivalent of input celsius degree = ", fahrenheit_temp)
輸出
執行上述程式將生成以下輸出 -
The input Temperature in Celsius is 80 The Fahrenheit equivalent of input celsius degree = 176.0
結論
在本文中,我們學習了什麼是攝氏溫度和華氏溫度。我們還學習瞭如何使用數學公式將它們進行轉換。我們還學習瞭如何編寫一個使用者定義函式,該函式以攝氏溫度作為引數,將其轉換為華氏度,並將其返回。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP