我的华氏温度到Celsius / Kelvin转换器有问题

时间:2013-03-08 01:17:46

标签: python math python-2.7

# Fahrenheit to Celcius    
def f2c():
    userInput = tempEntry.get().lower()
    thisEquation = "Fahrenheit to Celcius"
    if userInput == "":
        textWid.insert(END,"-- " + thisEquation + " --")
        textWid.insert(END,"\n")
        textWid.insert(END,temp_equations[thisEquation])
        textWid.insert(END,"\n")
        textWid.insert(END,"\n")
    elif userInput.isdigit():
        textWid.insert(END,"Fahrenheit = ")
        textWid.insert(END,str(((float(userInput) - 32) * (5/9))))
        textWid.insert(END,"\n")
    else:
        textWid.insert(END,"Invalid entry for"+" "+thisEquation)
        textWid.insert(END,"\n")

# Fahrenheit to Kelvin
def f2k():
    userInput = tempEntry.get().lower()
    thisEquation = "Fahrenheit to Kelvin"
    if userInput == "":
        textWid.insert(END,"-- " + thisEquation + " --")
        textWid.insert(END,"\n")
        textWid.insert(END,temp_equations[thisEquation])
        textWid.insert(END,"\n")
        textWid.insert(END,"\n")
    elif userInput.isdigit():
        textWid.insert(END,"Fahrenheit = ")
        textWid.insert(END,str(((5/9)*(float(userInput) - 32) + 273.15)))
        textWid.insert(END,"\n")
    else:
        textWid.insert(END,"Invalid entry for"+" "+thisEquation)
        textWid.insert(END,"\n")

userInput是全局定义的Tkinter Entry框。 我强烈怀疑我的问题源于两个方程,但我已经尝试过多次重复工作。

我的Fahrenheit到Celcius转换器总是返回0.0 华氏温度转换为开尔文转换器每次约20次。

完全难倒在这里的人,非常感谢任何帮助。

1 个答案:

答案 0 :(得分:8)

5 / 9是你的问题:

>>> 5 / 9
    0

在Python 2中,将整数除以整数会产生整数。你想让至少其中一个数字成为浮点数:

>>> 5.0 / 9
    0.5555555555555556
>>> 5.0 / 9.0
    0.5555555555555556