Float division issue in Python (tkinter)

Question:

I’m working on a program which uses inputs from a tkinter interface to calculate the relative funniness of a joke based on sciencewritenow.com’s formula:

H ≥ ((((T1 + T2) / (T3 – R)) × I ) / D = ((T3 / T4) × S)) × B

While I haven’t encountered too many problems, whenever I input the numbers and hit the button to calculate the humour, I get the following error:

H = (((float(T1.get()) + (float(T2.get()) * 0.1)) / (float(T3.get()) - float(R.get())) * float(I.get())) / float(D)) * float(B)
ZeroDivisionError: float division by zero

I guess it’s pretty self-explanatory: the formula is trying to divide something by zero at some point in the program. The issue is that I didn’t input 0 at any point in the program, and I don’t think that any of the calculations should total zero. So, unless python has an issue with me inputing decimals like 0.33 (which it shouldn’t, since I converted all the variables to floats), then I don’t see what the issue is.

Here’s my code (You can probably ignore the bottom half of the program, because I doubt that the issue is there:

import tkinter as tk

H = 0

# H ==> humour (greater than/equal to 1 = successful)
# T1 ==> tragedy (1-10, increasing)
# T2 ==> time (0.1 = 1 month)
# T3 ==> topicality (1-10, increasing)
# R ==> risk of recurrence (1-5, decreasing)
# I ==> immmunity (0, 0.025, 0.5, 0.75, 1)
# D ==> delivery ((T3 / T4) × S)) × B
# T4 ==> timing (0.5 = fast, 1 = slow)
# T5 ==> tone (0.33 = serious, 0.66 = conversational, 0.99 = lighthearted)
# S ==> shock value (1-3, increasing)
# B ==> bastard effect (0.5, unchanging)

def addNums():
    D = ((float(T4.get()) / float(T5.get())) * float(S.get()))
    
    H = (((float(T1.get()) + (float(T2.get()) * 0.1)) / (float(T3.get()) - float(R.get())) * float(I.get())) / float(D)) * float(B)
    
    print(H)
    
    result = tk.Label(text=(str(H), "funnies"))
    result.pack()

window = tk.Tk()
window.title("The Funny Formula")
window.geometry("300x300")

header = tk.Label(text="Input the level of tragedy (1-10)")
header.pack()
T1 = tk.Entry(text = "")
T1.pack()

header = tk.Label(text="Input the amount of time since the tragedy (1 per month)")
header.pack()
T2 = tk.Entry(text = "")
T2.pack()

header = tk.Label(text="Input the topicality (1-10)")
header.pack()
T3 = tk.Entry(text = "")
T3.pack()

header = tk.Label(text="Input the risk of recurrence (5-1)")
header.pack()
R = tk.Entry(text = "")
R.pack()

header = tk.Label(text="Input the immunity (0.25 intervals)")
header.pack()
I = tk.Entry(text = "")
I.pack()

header = tk.Label(text="Input the timing (0.5 = fast, 1 = slow)")
header.pack()
T4 = tk.Entry(text = "")
T4.pack()

header = tk.Label(text="Input the tone (0.33, 0.66, 0.99)")
header.pack()
T5 = tk.Entry(text = "")
T5.pack()

header = tk.Label(text="Input the shock value (1-3)")
header.pack()
S = tk.Entry(text = "")
S.pack()

B = 0.5


button1 = tk.Button(text='Find the funny', command=addNums)
button1.pack()


tk.mainloop()

I tried converting the variables to strings, which obviously didn’t work and was a long shot. I also calculated the inputs manually on a calculator and couldn’t find an issue.

Answers:

def addNums():
    print(T4.get(), T5.get(), S.get())
    D = ((float(T4.get()) / float(T5.get())) * float(S.get()))
    print(D)
    
    H = (((float(T1.get()) + (float(T2.get()) * 0.1)) / (float(T3.get()) - float(R.get())) * float(I.get())) / float(D)) * float(B)
    
    print(H)

    result = tk.Label(text=(str(H), "funnies"))
    result.pack()

Actually this error is basically "float division by zero". So actually when calculating H division is happening because D is calcualting on the basis of T4,T5 or S. Which seems that T4 value is divided by T5. So if T5 is zero than issue occurs.

Answered By: Iqbal Hussain