Bra*_*eon 3 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")
Run Code Online (Sandbox Code Playgroud)
userInput是全局定义的Tkinter Entry框.我强烈怀疑我的问题源于两个方程式,但我已经尝试过多次重新处理它们.
我的Fahrenheit到Celcius转换器总是返回0.0华氏温度到开尔文转换器每次约20次.
完全难倒在这里的家伙,任何帮助将不胜感激.
5 / 9 是你的问题:
>>> 5 / 9
0
Run Code Online (Sandbox Code Playgroud)
在Python 2中,将整数除以整数会产生整数.您想要使至少一个数字成为浮点数:
>>> 5.0 / 9
0.5555555555555556
>>> 5.0 / 9.0
0.5555555555555556
Run Code Online (Sandbox Code Playgroud)