so i am making a temp converter and i just cant get it to the calc for Fahrenheit to Celsius. It works to go from Celsius to Fahrenheit but it wont convert the other way. I am not sure what problem is causing this i have tried the intvars and stringvars but none seem to fix this problem and i have also tried the diff if statements but its just cant convert from fah to cel. Could someone please help me and tell me whats wrong. Here is what i have:
from Tkinter import*
def convert():
celTemp = celTempVar.get()
fahTemp = fahTempVar.get()
if celTempVar.get() != 0.0:
celToFah = (celTemp * 9/5 + 32)
print celToFah
fahTempVar.set(celToFah)
elif fahTempVar.get() != 0.0:
fahToCel = ((fahTemp - 32) * (5/9))
print fahToCel
celTempVar.set(fahToCel)
def reset():
top = Toplevel(padx=50, pady=50)
top.grid()
message = Label(top, text = "Reset Complete")
button = Button(top, text="OK", command=top.destroy)
message.grid(row = 0, padx = 5, pady = 5)
button.grid(row = 1, ipadx = 10, ipady = 10, padx = 5, pady = 5)
fahTempVar.set(int(0))
celTempVar.set(int(0))
###MAIN###
root = Tk()
root.title("Temperature Converter")
mainframe = Frame(root)
mainframe.grid()
celTempVar = IntVar()
celTempVar.set(int(0))
fahTempVar = IntVar()
fahTempVar.set(int(0))
titleLabel = Label (mainframe, text = "Celcius/Fahrenheit Temperature", font = ("Arial", 20, "bold"), justify = CENTER)
titleLabel.grid(row = 1, column = 1, columnspan = 3, pady = 10, padx = 20)
celLabel = Label (mainframe, text = "Celcius: ", font = ("Arial", 16), fg = "red")
celLabel.grid(row = 2, column = 1, pady = 10, sticky = NW)
fahLabel = Label (mainframe, text = "Fahrenheit: ", font = ("Arial", 16), fg = "blue")
fahLabel.grid(row = 3, column = 1, pady = 10, sticky = NW)
celEntry = Entry (mainframe, width = 10, bd = 5, textvariable = celTempVar)
celEntry.grid(row = 2, column = 1, pady = 10, sticky = NW, padx = 125 )
fahEntry = Entry (mainframe, width = 10, bd = 5, textvariable = fahTempVar)
fahEntry.grid(row = 3, column = 1, pady = 10, sticky = NW, padx = 125 )
convertButton =Button (mainframe, text = "Convert", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command = convert)
convertButton.grid(row = 4, column = 1, ipady = 8, ipadx = 12, pady = 5, sticky = NW, padx = 55)
resetButton = Button (mainframe, text = "Reset", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command = reset)
resetButton.grid(row = 5, column = 1,ipady = 8, ipadx = 12, pady = 5, sticky = NW)
root.mainloop()
The problem is this line:
fahToCel = ((fahTemp - 32) * (5/9))
The issue is that the 5/9
is computed using integer division (because both inputs to it are integers); it happens to be equal to 0
, which isn't what you want here! There's a whole bunch of possible fixes, but the simplest is to just use 5.0/9.0
which is unambiguously dealing with floating-point division. (You might as well change the opposite direction too, even if just for clarity. Yes, it is currently working but this is because the multiply — which has a float input of celTemp
— is processed before the divide there.)