请输入温度:30C
摄氏温度 = 30.0,华氏温度 = 86.0
请输入温度:86F
华氏温度 = 86.0,摄氏温度 = 30.0
(图二)
代码:
class Temperature:
def __init__(self,degree):
self.degree = degree[-1]
self.num = degree[0:-1]
def to_fab(self):
if self.degree in 'F':
c = (eval(self.num)-32)/1.8
return c
else:
return eval(self.num)
def to_cel(self):
if self.degree in 'C':
f = 1.8*eval(self.num)+32
return f
else:
return eval(self.num)
while True:
deg = input("请输入温度:")
dc1 = Temperature(deg)
print("摄氏温度=","{:.1f}".format(dc1.to_fab()),end=",")
print("华氏温度=","{:.1f}".format(dc1.to_cel()))