-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
61 lines (53 loc) · 1.75 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.title("BMI Calculator")
window.config(padx=90, pady=90)
def calculate_bmi():
height = heightInput.get()
weight = weightInput.get()
if weight == "" or height == "":
resultLabel.config(text="Enter both weight and height")
else:
try:
height = float(height)
weight = float(weight)
bmi = weight / ((height / 100) ** 2)
resultString = writeResult(bmi)
resultLabel.config(text=resultString)
except ValueError:
resultLabel.config(text="Enter a valid number!")
messagebox.showerror("Invalid Input", "Please enter valid numeric values for weight and height.")
# UI
weightInputLabel = tk.Label(text="Enter your weight (kg)")
weightInputLabel.pack()
weightInput = tk.Entry(width=10)
weightInput.pack()
heightInputLabel = tk.Label(text="Enter your height (cm)")
heightInputLabel.pack()
heightInput = tk.Entry(width=10)
heightInput.pack()
calculateButton = tk.Button(text="Calculate", command=calculate_bmi)
calculateButton.pack()
resultLabel = tk.Label()
resultLabel.pack()
def writeResult(bmi):
resultString = f"Your BMI is {round(bmi, 2)}, You are "
if bmi <= 16:
resultString += "severely thin"
elif 16 < bmi <= 17:
resultString += "moderately thin"
elif 17 < bmi <= 18.5:
resultString += "mildly thin"
elif 18.5 < bmi <= 25:
resultString += "normal weight"
elif 25 < bmi <= 30:
resultString += "overweight"
elif 30 < bmi <= 35:
resultString += "obese class 1"
elif 35 < bmi <= 40:
resultString += "obese class 2"
else:
resultString += "obese class 3"
return resultString
window.mainloop()