python中温度单位如何转换?
python中温度单位如何转换?
温度有摄氏度和华氏度两个单位,我们通常使用的是摄氏度,对于转换成华氏度,很多小伙伴记不住公式。作为万能的计算机,它是可以帮助我们解决温度单位转换的问题。本文主要演示python中进行温度单位转换的代码过程,具体请看本文。
一、问题
温度有摄氏度(Celsius)和华氏度(Fabrenheit)两个不同的单位。摄氏度0度为结冰点,沸点为100度;华氏度以32度为冰点,以212度为沸点。一般来说,中国采用摄氏度,美国采用华氏度。
两者之间的转换公式为:摄氏度=(华氏度-32)/1.8、华氏度=摄氏度*1.8+32。
二、代码
输入
#定义一个函数获取带符号的温度值。
deftempstr():
whileTrue:
temp=input('请输入带有符号[C代表摄氏度,F代表华氏度]的温度数值:')
iftemp[-1]in['c','C','f','F']:
returntemp
else:#如果输入的温度值没有带有符号,会提示输入错误并被要求重新输入。
print('输入错误,请输入带有符号的温度数值')
print('-'*20)
处理输出
#定义一个函数获取带符号的温度值。
deftempstr():
whileTrue:
temp=input('请输入带有符号[C代表摄氏度,F代表华氏度]的温度数值:')
iftemp[-1]in['c','C','f','F']:
returntemp
else:#如果输入的温度值没有带有符号,会提示输入错误并被要求重新输入。
print('输入错误,请输入带有符号的温度数值')
print('-'*20)
总体代码
deftempstr():
whileTrue:
temp=input('请输入带有符号[C代表摄氏度,F代表华氏度]的温度数值:')
iftemp[-1]in['c','C','f','F']:
returntemp
else:
print('输入错误,请输入带有符号的温度数值')
print('-'*20)
defprogress(temp):
iftemp[-1]in['F','f']:
output=(eval(temp[:-1])-32)/1.8
print('温度转换为摄氏度为{:.2f}C'.format(output))
else:
output=eval(temp[:-1])*1.8+32
print('温度转换为华氏度为{:.2f}F'.format(output))
temp=tempstr()
progress(temp)
以上就是python中进行温度单位转换的代码过程,大家可以直接套用使用哦~更多Python学习教程请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python中for _ in range(len)
Python中的for _ in range(len)是一种循环语句,它可以让程序员在循环中对列表、元组、字符串等数据类型进行遍历。在这种循环语句中,_代表一个...详情>>
2023-11-15 22:06:34
python中for in range的用法
Python中的for循环是一种重要的控制结构,它可以用来遍历可迭代对象中的元素。其中,for in range是for循环的一种常用形式,它可以在指定范围内...详情>>
2023-11-15 20:59:52
python range用法
Python中的range()函数是一个非常强大的工具,它可以生成一个数字序列,让我们可以很方便地进行循环和迭代操作。我们将深入探讨range()函数的用...详情>>
2023-11-15 20:35:16
python complex函数
Python complex函数是Python中的一个内置函数,用于创建一个复数。复数是由实部和虚部组成的数学对象,其中实部和虚部都是浮点数。在Python中,...详情>>
2023-11-15 19:59:25热门推荐
python中if函数多个条件并列
沸python中for _ in range(len)
热python中for range
热python中for in range的用法
新python range用法
python complex函数
int函数python作用举例
python中字符串与字典相互转换的方法
python中如何用remove函数进行集合删除操作?
python中温度单位如何转换?
Python中如何使用replace()方法实现字符串内部替换?
python中glob库是什么?
python中如何创建带有文件名的临时文件?
python中shell的调用
技术干货






