-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmi.py
More file actions
60 lines (47 loc) · 1.5 KB
/
bmi.py
File metadata and controls
60 lines (47 loc) · 1.5 KB
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
def calculate_bmi(height, weight):
"""
Calculate BMI given height (in meters) and weight (in kilograms).
Args:
height (float): Height in meters.
weight (float): Weight in kilograms.
Returns:
float: Calculated BMI.
"""
try:
bmi = round(weight / (height**2), 2)
return bmi
except ZeroDivisionError:
return None
def interpret_bmi(bmi):
"""
Interpret the BMI and provide a classification.
Args:
bmi (float): Calculated BMI.
Returns:
str: BMI interpretation.
"""
if bmi is None:
return "Invalid input. Height should be greater than 0."
if bmi < 18.5:
return "Your BMI is {bmi}, you are underweight."
elif bmi < 24.9:
return "Your BMI is {bmi}, you have a normal weight."
elif bmi < 29.9:
return "Your BMI is {bmi}, you are overweight."
elif bmi < 34.9:
return "Your BMI is {bmi}, you are obese (Class I)."
elif bmi < 39.9:
return "Your BMI is {bmi}, you are obese (Class II)."
else:
return "Your BMI is {bmi}, you are obese (Class III)."
def main():
try:
height = float(input("Enter your height in meters: "))
weight = float(input("Enter your weight in kilograms: "))
bmi = calculate_bmi(height, weight)
result = interpret_bmi(bmi)
print(result)
except ValueError:
print("Invalid input. Please enter numerical values for height and weight.")
if __name__ == "__main__":
main()