-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmi.py
34 lines (29 loc) · 1.21 KB
/
bmi.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
def calculate_bmi(weight, height, metric='metric'):
"""Calculate the Body Mass Index (BMI) of a person."""
if metric == 'english':
# Convert pounds to kilograms
weight_kg = weight * 0.453592
# Convert feet and inches to meters
height_inches = height[0] * 12 + height[1]
height_m = height_inches * 0.0254
else:
weight_kg = weight
height_m = height
return weight_kg / (height_m ** 2)
def get_input(metric):
if metric == 'english':
weight = float(input("Enter weight in pounds: "))
feet = int(input("Enter height in feet: "))
inches = int(input("Enter height in inches: "))
height = (feet, inches)
else:
weight = float(input("Enter weight in kilograms: "))
height = float(input("Enter height in meters: "))
return weight, height
metric_choice = input("Choose metric system (english/metric): ").lower()
if metric_choice not in ['english', 'metric']:
print("Invalid choice. Using metric system by default.")
metric_choice = 'metric'
weight, height = get_input(metric_choice)
bmi = calculate_bmi(weight, height, metric_choice)
print(f"Your BMI is: {bmi:.2f}")