forked from linuxacademy/content-python3-sysadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmi
executable file
·35 lines (30 loc) · 1.09 KB
/
bmi
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
#!/usr/bin/env python3.6
# BMI = (weight in kg / height in meters squared)
# Imperial version: BMI * 703
def gather_info():
height = float(input("What is your height? (inches or meters) "))
weight = float(input("What is your weight? (pounds or kilograms) "))
system = input("Are your measurements in metric or imperial units? ").lower().strip()
return (height, weight, system)
def calculate_bmi(weight, height, system='metric'):
"""
Return the Body Mass Index (BMI) for the
given weight, height, and measurement system.
"""
if system == 'metric':
bmi = (weight / (height ** 2))
else:
bmi = 703 * (weight / (height ** 2))
return bmi
while True:
height, weight, system = gather_info()
if system.startswith('i'):
bmi = calculate_bmi(weight, system=system, height=height)
print(f"Your BMI is {bmi}")
break
elif system.startswith('m'):
bmi = calculate_bmi(weight, height)
print(f"Your BMI is {bmi}")
break
else:
print("Error: Unknown measurement system. Please use imperial or metric.")