通过输入摄氏温度及需要转换的类型,将摄氏温度转换为其他类型温度。
temp_type = ['摄氏温度', '华氏温度', '开氏温度', '列氏温度', '兰金温度']
def is_digit(temp):
try:
float(temp)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(temp)
return True
except (TypeError, ValueError):
pass
return False
def temperature_trans(temp, temp_type):
if temp_type == '1':
result = float(temp)
elif temp_type == '2':
result = float(temp) * 1.8 + 32
elif temp_type == '3':
result = float(temp) + 273.15
elif temp_type == '4':
result = float(temp) * 0.8
elif temp_type == '5':
result = (float(temp) + 237.15) * 1.8
return result
if __name__ == '__main__':
print('*' * 40)
print(' ' * 12, "摄氏温度转换器", ' ' * 17)
print('*' * 40)
while True:
temp = input("Input temperature('q' to quit):")
if temp == 'q':
break
if not is_digit(temp):
print("Please input the right centigrade!\n")
continue
temp_type_index = input("Input destination temperature type:\n"
"1 for '摄氏温度'\n 2 for '华氏温度'\n "
"3 for '开氏温度'\n 4 for '列氏温度'\n "
"5 for '兰金温度'\n Type=")
result = temperature_trans(temp, temp_type_index)
trans_result = temp_type[int(temp_type_index) - 1] + " {:.2f} ".format(result)
print(trans_result)