3
# 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 输入框。我强烈怀疑我的问题源于这两个方程,但我已经尝试过多次重新处理它们。

我的华氏度到摄氏度的转换器总是返回 0.0 华氏度到开尔文的转换器每次大约 20 折。

完全难倒这里的家伙,任何帮助将不胜感激。

4

1 回答 1

8

5 / 9是你的问题:

>>> 5 / 9
    0

在 Python 2 中,将整数除以整数会产生整数。您希望至少有一个数字为浮点数:

>>> 5.0 / 9
    0.5555555555555556
>>> 5.0 / 9.0
    0.5555555555555556
于 2013-03-08T01:19:49.117 回答