华氏度和摄氏度的转换关系为,℉ = 9×℃ /5+32 或 ℃ = 5×(℉- 32)/9

输入为一个字符串,最后一位62616964757a686964616fe4b893e5b19e31333337396334为'F'表示输入为华氏度,最后一位为'C'表示输入为摄氏度

输出将自动转换成为相同格式的另一种温度。示例的输入为:'30.1C'、'86F'。

def tempTransform(tempStr):
tempVal = tempStr[:-1]
try:
tempVal = float(tempVal)
except ValueError:
raise ValueError('Temperature value is not valid.')
tempUnit = tempStr[-1]
if tempUnit == 'F':
tempVal = (tempVal - 32) * 5 / 9
return '{}C'.format(tempVal)
elif tempUnit == 'C':
tempVal = tempVal * 9 / 5 + 32
return '{}F'.format(tempVal)
else:
raise ValueError('Temperature unit is not valid.')
print(tempTransform('30.1C'))
# 86.18F

如果输入字符串中的温度值无效或者单位不是'C'或者'F',均会会抛出ValueError。