基于Python实现温度转换程序
1.使用pycharm运行温度转换程序,尝试将温度单位设在前面
2.参照温度转换程序,自己写一个关于货币转换、长度转换、重量转换或者面积转换的程序
循环+函数
def convertemperature():
temperature = ""
while (temperature != "q"):
temperature = input("请输入带有符号的温度:")
if (temperature[-1] in ['f', 'F']):
C = (eval(temperature[0:-1]) - 32) / 1.8
print("转换后的温度是{:.2f}C".format(C))
print("退出请输入q")
elif (temperature[-1] in ['c', 'C']):
C = (1.8 * eval(temperature[0:-1]) + 32)
print("转换后的温度是{:.2f}C".format(C))
else:
print("输入格式错误")
print("退出请输入q")
convertemperature()循环 +异常处理 温度单位设在前面 f代表华氏度 c代表摄氏度---函数里面循环
def convertemperature():
temperature = ""
while (temperature != "exit"):
temperature = input("请输入带有符号的温度:")
if (temperature[0] in ['f', 'F']):
try:
C = (eval(temperature[1:]) - 32) / 1.8
print("转换后的温度是{:.2f}C".format(C))
except:
print("转换失败,请重新输入")
print("退出请输入exit")
elif (temperature[0] in ['c', 'C']):
try:
C = (1.8 * eval(temperature[1:]) + 32)
print("转换后的温度是{:.2f}C".format(C))
except:
print("转换失败,请重新输入")
print("退出请输入exit")
else:
print("输入格式错误")
print("退出请输入exit")
# 循环 +异常处理 温度单位设在前面 f代表华氏度 c代表摄氏度
convertemperature()循环里面使用函数
temperature=""
while (temperature != "q"):
temperature = input("请输入带有符号的温度:")
convertemperature2(temperature)
def convertemperature2(aa):
temperature = aa
if (temperature[0] in ['f', 'F']):
try:
C = (eval(temperature[1:]) - 32) / 1.8
print("转换后的温度是{:.2f}C".format(C))
except:
print("转换失败,请重新输入")
print("退出请输入q")
elif (temperature[0] in ['c', 'C']):
try:
C = (1.8 * eval(temperature[1:]) + 32)
print("转换后的温度是{:.2f}C".format(C))
except:
print("转换失败,请重新输入")
print("退出请输入q")
else:
print("输入格式错误")
print("退出请输入q")重量转换
# 货币转换、长度转换 、重量转换或者面积转换
temperature = ""
while temperature != "q":
temperature = input("请输入带有符号的重量:")
print(temperature[-2:])
if temperature[-2:] in ['kg', "Kg", "KG", "kG"]:
try:
C = (eval(temperature[0:-2]) * 1000)
print("转换后的重量是{:.2f}克".format(C))
except:
print("转换失败,请重新输入")
print("退出请输入q")
elif temperature[-1] in ['g', 'G']:
try:
C = (eval(temperature[0:-1]) / 1000)
print("转换后的重量是{:.2f}千克".format(C))
except:
print("转换失败,请重新输入")
print("退出请输入q")
else:
print("输入格式错误")
print("退出请输入q")
# 循环 +异常处理 重量转换模仿例子程序,编写英寸和厘米的转换程序,1inch=2.54cm。比如,输入3.2i则输出8.13 c,输入4.5c则输出1.77i,输入4.5k输出“输入格式错误”。
temperature = ""
while temperature != "q":
temperature = input("请输入带有符号的尺寸:eg:3.2i \n")
if temperature[-1:] in ['i', 'I']:
try:
C = (eval(temperature[0:-1]) * 2.54)
print("转换后是{:.2f}c".format(C))
except:
print("转换失败,请重新输入")
print("退出请输入q")
elif temperature[-1] in ['c', 'C']:
try:
C = (eval(temperature[0:-1]) / 2.54)
print("转换后是{:.2f}i".format(C))
except:
print("转换失败,请重新输入")
print("退出请输入q")
else:
print("输入格式错误")
print("退出请输入q")到此这篇关于基于Python实现温度转换程序的文章就介绍到这了,更多相关Python温度转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
python可视化 matplotlib画图使用colorbar工具自定义颜色
这篇文章主要介绍了python可视化 matplotlib画图使用colorbar工具自定义颜色,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-12-12
Python常用模块sys,os,time,random功能与用法实例分析
这篇文章主要介绍了Python常用模块sys,os,time,random功能与用法,结合实例形式分析了Python模块sys,os,time,random功能、原理、相关模块函数、使用技巧与操作注意事项,需要的朋友可以参考下2020-01-01
Python报错TypeError: unhashable type: ‘numpy.nd
在Python编程中,尤其是在处理数据时,我们经常使用numpy数组,然而,当我们尝试将numpy数组用作字典的键或集合的元素时,就会遇到TypeError: unhashable type: 'numpy.ndarray',本文将探讨这个错误的原因,并给出几种可能的解决方案,需要的朋友可以参考下2024-09-09
python中的hashlib和base64加密模块使用实例
这篇文章主要介绍了python中的hashlib和base64加密模块使用实例,hashlib模块支持的加密算法有md5 sha1 sha224 sha256 sha384 sha512,需要的朋友可以参考下2014-09-09


最新评论