获取用户输入的温度值
temp_str = input("请输入带有符号的温度值(例如:32C 或 F32):")
判断温度值的单位
if temp_str[-1] in ['C', 'c']:
摄氏温度转华氏温度
fahrenheit = 1.8 * float(temp_str[0:-1]) + 32
print("转换后的温度是:{:.2f}F".format(fahrenheit))
elif temp_str[-1] in ['F', 'f']:
华氏温度转摄氏温度
celsius = (float(temp_str[0:-1]) - 32) / 1.8
print("转换后的温度是:{:.2f}C".format(celsius))
else:
print("输入格式错误,请确保输入的温度值以C或F结尾。"
这个程序首先获取用户输入的温度值,然后根据温度值的单位(C或F)进行转换,并输出转换后的温度值。如果输入的温度值不符合格式要求,程序会提示输入格式错误。