温度转换python功能

时间:2016-01-21 23:17:23

标签: python

我试图找到解决这个问题的解决方案已经有2个小时了,我不知道该怎么输入这样的函数?

num = input ('Which Degree Temperature would you like to convert? ')
corf = raw_input ('Is the temperature Fahrenheit Degree or is it Celsius?(Type "f" for Fahrenheit or "c" for Celsius)')
def max(num,corf):
    if corf == 1 :
        return num
        Celsius = (num - 32) * 5.0 / 9.0
        print str(num) + " Fahrenheit is equal to " + str(Celsius)+ " Celsius or " + ('%0.1f degrees Celsius ' %(Celsius)) + 'to one decimal place '
    elif corf == 2 :
        return num
        Fahrenheit = (num * 9.0 / 5.0 ) + 32  
        print str(num) + " Celsius is equal to " + str(Fahrenheit)+ " Fahrenheit or " + ('%0.1f degrees Celsius ' %(Celsius)) + 'to one decimal place '
    else:
        return 'Error'

1 个答案:

答案 0 :(得分:1)

在每个ifelif语句中,将数字更改为正确的字符串:

if corf == "f":        
    return num
    Celsius = (num - 32) * 5.0 / 9.0
    print str(num) + " Fahrenheit is equal to " + str(Celsius)+ " Celsius or " + ('%0.1f degrees Celsius ' %(Celsius)) + 'to one decimal place '
elif corf == "c":
    return num
    Fahrenheit = (num * 9.0 / 5.0 ) + 32  
    print str(num) + " Celsius is equal to " + str(Fahrenheit)+ " Fahrenheit or " + ('%0.1f degrees Celsius ' %(Celsius)) + 'to one decimal place '

为什么呢?因为corf的唯一输入应该只是" f"或" c"。现在,每个条目都应与正确的代码匹配,如上所示。