2019-03-24 21:59:57 +08:00

24 lines
861 B
Python

import re
def trns_degree(s):
l = re.match(r'(-?[0-9]*\.?[0-9]*)\ *(K|C|F|Ra|R)', s)
if not l:
return '输入格式有误!'
d, m = float(l.group(1)), ('K', 'C', 'F', 'Ra', 'R').index(l.group(2))
u = ('开尔文', '摄氏度', '华氏度', '兰氏度', '列氏度')
k = (lambda x: x,
lambda x: x + 273.15,
lambda x: (x + 459.67) / 1.8,
lambda x: x / 1.8,
lambda x: x * 1.25 + 273.15)[m](d)
t = (lambda x: x,
lambda x: x - 273.15,
lambda x: x * 1.8 - 459.67,
lambda x: x * 1.8,
lambda x: (x - 273.15) * 0.8)
return '输入温度为 {d} {p},转换如下:\n'.format(d=d, p=u[m]) + \
'\n'.join(['{d:8.2f} {p}'.format(d=t[i](k), p=u[i]) for i in range(len(t))])
print(trns_degree(input('请输入一个温度,以 "K","C","F","Ra","R" 结尾:')))