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
步骤
下面是执行所需任务的算法/步骤:
- 创建一个变量来存储输入的摄氏度温度 celsius 。
-
使用数学公式 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
结论
在本文中,我们学习了摄氏温度和华氏温度的概念,并学会了使用数学公式进行转换。我们还学会了如何编写一个用户定义的函数,以摄氏温度作为参数,将其转换为华氏温度并返回结果。