1摄氏华氏温度转换转换公式是:摄氏温度=华氏温度-32÷18输入输出数据的格式为温度标识符在前如:C82 F28。
摄氏华氏温度转换的代码如下:
def celsius_to_fahrenheit(celsius):
fahrenheit = celsius * 1.8 + 32
return fahrenheit
def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) / 1.8
return celsius
temperature = input("请输入温度(格式为温度标识符在前,如:C82, F28):")
if temperature[0] == 'C':
celsius = int(temperature[1:])
fahrenheit = celsius_to_fahrenheit(celsius)
print("华氏温度为:F" + str(fahrenheit))
elif temperature[0] == 'F':
fahrenheit = int(temperature[1:])
celsius = fahrenheit_to_celsius(fahrenheit)
print("摄氏温度为:C" + str(celsius))
else:
print("输入格式有误,请重新输入。")
运行代码后,用户可以输入温度(格式为温度标识符在前,如:C82, F28),程序会根据输入的温度标识符进行相应的转换计算,并输出相应的结果。如果输入的格式有误,程序会提示用户重新输入。
原文地址: http://www.cveoy.top/t/topic/jgDh 著作权归作者所有。请勿转载和采集!