Python Forum
[Tkinter] rez = (mass / h) | ZeroDivisionError
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] rez = (mass / h) | ZeroDivisionError
#1
Before I post here I did some research, but still, I can't fix the problem. Are the entry fields both 0? How can I change the one of the values of the entry so the app can start or there is another way. Thank you!


from tkinter import *

root = Tk()
root.title('BMI Calculator')
root.geometry('180x200+800+400')


def bmi():
    get_height = ent_height.get()
    height = int(get_height)
    get_mass = ent_weight.get()
    mass = int(get_mass)

    h = (height * height)/1000
    rez = (mass / h)
    print(rez)

    if rez < 18.5:
        result_lbl["text"] = rez
        print("You are Underweight")
    elif rez >= 18.5 and rez < 24.9:
        print("You have Normal Weight")
    elif rez >= 25 and rez <=29.9:
        print("You are Overweight")
    elif rez > 30:
        print("You are Obese!!!")

# Create Frame
frame = Frame(height = 550)
frame.pack(fill=X)
# Create Label and Entry for Height
lbl_height = Label(frame, text = 'What is your height(Ex.180cm) ?')
lbl_height.grid(row= 0, column = 0)
ent_height = IntVar()
ent_height = Entry(frame, width=30, textvariable=ent_height)
ent_height.grid(row=1, column = 0)

# Create Label and Entry for Weight
lbl_weight = Label(frame, text='What is your weight(kg)?')
lbl_weight.grid(row= 2, column = 0)
ent_weight = IntVar(value=1)
ent_weight = Entry(frame, width = 30, textvariable=ent_weight)
ent_weight.grid(row = 3, column = 0)

# Button to calculate everything
btnCalc = Button(frame, text = 'Calculate', command = bmi())
btnCalc.grid(row = 5, column = 0)

result_lbl = Label(frame, Variable=bmi())



root.mainloop()
Reply
#2
You are looking for a way to initialize entries in your form, but that was not the real problem. You should still fix your bmi calculation to check for a zero height instead of crashing. That is sloppy programming because there will be times when the program gets executed and the height is set to zero. But that isn't the real problem here. You should be wondering why the bmi function is being called even though you didn't press the calculate button. I bet there may be a problem with binding the bmi() function to the button

I cannot tell you how often I run into this same sort of thing working with professional programmers. You look where the program crashes thinking that is the problem. About half the time that is only where the program finally results in a program crash. The real problem may have happened in a different file and a different time, but it is hard to see past the stack trace to spot the real errant behavior. It is hard to see the forest for the trees.
Reply
#3
(Oct-20-2020, 04:18 AM)deanhystad Wrote: You are looking for a way to initialize entries in your form, but that was not the real problem. You should still fix your bmi calculation to check for a zero height instead of crashing. That is sloppy programming because there will be times when the program gets executed and the height is set to zero. But that isn't the real problem here. You should be wondering why the bmi function is being called even though you didn't press the calculate button. I bet there may be a problem with binding the bmi() function to the button

I cannot tell you how often I run into this same sort of thing working with professional programmers. You look where the program crashes thinking that is the problem. About half the time that is only where the program finally results in a program crash. The real problem may have happened in a different file and a different time, but it is hard to see past the stack trace to spot the real errant behavior. It is hard to see the forest for the trees.

I appreciate your feedback. I have only 1-2 months of experience with Python, small errors like this can't hold me for a few days till I find the solution, which is not the case with experinced programmers Smile
Reply
#4
No, it is still the case with experienced programmers. If fact it can be a lot worse with experienced programmers because we fall into the trap of thinking we know stuff. If you think you know stuff you stop really looking because "I know that part works" or "that part is not related to this issue."
Reply
#5
(Oct-20-2020, 07:04 PM)deanhystad Wrote: No, it is still the case with experienced programmers. If fact it can be a lot worse with experienced programmers because we fall into the trap of thinking we know stuff. If you think you know stuff you stop really looking because "I know that part works" or "that part is not related to this issue."

Even I didn't fix all errors, the app at least is working. I have one curious question about your experience with python. How long it took for you to have decent knowledge about python and be able to develop anything you want?


[Image: bmi-calc.png]
Reply
#6
Let you know when I get there.

"Obese!" is body shaming.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ZeroDivisionError: float division by zero Jionni 8 4,609 Feb-19-2020, 09:11 PM
Last Post: Jionni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020