如何使用 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

結論

在本文中,我們學習了什麼是攝氏溫度和華氏溫度。我們還學習瞭如何使用數學公式將它們進行轉換。我們還學習瞭如何編寫一個使用者定義函式,該函式以攝氏溫度作為引數,將其轉換為華氏溫度並返回。

更新於:2022年10月25日

16K+ 閱讀量

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.