print("Lets calculate your BMI!\n")
valid_region = False
while valid_region == False:
#prompts for user's region/race
user_region = input("Enter ‘1’ for US/Europe or ‘2’ for Asian as the \
selected region/race. Please enter the numeric value only without any leading \
or trailing spaces.: ")
if user_region == '1' or user_region == '2':
valid_region = True
#prompts for user's preferred system of measurement
valid_system = False
while valid_system == False:
preferred_system = input("\nEnter ‘1’ for Metric (i.e. kg & m) or \
‘2’ for Imperial (i.e. lbs & inches) as the preferred measurement system. \
Please enter the numeric value only without any leading or trailing spaces.: ")
if preferred_system == '1' or preferred_system == '2':
valid_system = True
#if Metric is chosen
if preferred_system == '1':
valid_weight = False
while valid_weight == False:
#prompts for Metric weight in kilograms
user_weight = input("\nEnter your weight in kilograms \
(kg). Please enter a numeric value only. Decimals are allowed. Do not \
include any leading or trailing spaces. (e.g. '53.5'): ")
try:
weight_float = float(user_weight)
if weight_float > 0:
valid_weight = True
valid_height = False
while valid_height == False:
#prompts for Metric height in meters
user_height = input("\nEnter your height \
in meters (m). Please enter a numeric value only. Decimals are allowed. Do \
not include any leading or trailing spaces. (e.g. '1.7'): ")
try:
height_float = float(user_height)
if height_float > 0:
valid_height = True
#calculate BMI using Metric formula
metric_bmi = weight_float / \
(height_float ** 2)
if user_region == '1':
if metric_bmi < 18.5:
category = "underweight"
elif 18.5 <= metric_bmi < 25:
category = "normal"
elif 25 <= metric_bmi < 30:
category = "overweight"
else:
category = "obese"
elif user_region == '2':
if metric_bmi < 18.5:
category = "underweight"
elif 18.5 <= metric_bmi < 23:
category = "normal"
elif 23 <= metric_bmi < 25:
category = "overweight"
else:
category = "obese"
print("\nYour calculated BMI is \
{:.2f} kg/m^2. Based on the result, you are categorized as \
'{}'.".format(metric_bmi, category))
except ValueError:
print("\nInvalid input for height. \
Please try again!\n")
except ValueError:
print("\nInvalid input for weight. Please try \
again!\n")
#if Imperial is chosen
if preferred_system == '2':
valid_weight = False
while valid_weight == False:
#prompts for Imperial weight in pounds
user_weight = input("\nEnter your weight in pounds \
(lbs). Please enter a numeric value only. Decimals are allowed. Do not \
include any leading or trailing spaces. (e.g. '118'): ")
try:
weight_float = float(user_weight)
if weight_float > 0:
valid_weight = True
valid_height = False
while valid_height == False:
#prompts for Imperial height in inches
user_height = input("\nEnter your height \
in inches (in). Please enter a numeric value only. Decimals are allowed. Do \
not include any leading or trailing spaces. (e.g. '67'): ")
try:
height_float = float(user_height)
if height_float > 0:
valid_height = True
#calculate BMI - Imperial formula
imperial_bmi = \
(weight_float * 703) / \
(height_float ** 2)
#convert weight & height to Metric
weight_kg = weight_float * 0.453592
height_m = height_float * 0.0254
#calculate BMI - Metric formula
metric_bmi = weight_kg / \
(height_m ** 2)
if user_region == '1':
if metric_bmi < 18.5:
category = "underweight"
elif 18.5 <= metric_bmi < 25:
category = "normal"
elif 25 <= metric_bmi < 30:
category = "overweight"
else:
category = "obese"
elif user_region == '2':
if metric_bmi < 18.5:
category = "underweight"
elif 18.5 <= metric_bmi < 23:
category = "normal"
elif 23 <= metric_bmi < 25:
category = "overweight"
else:
category = "obese"
print("\nYour calculated BMI is \
{:.2f} lb/in^2. Based on the result, you are categorized as \
'{}'.".format(imperial_bmi, category))
except ValueError:
print("\nInvalid input for height. \
Please try again!\n")
except ValueError:
print("\nInvalid input for weight. Please try \
again!\n")
else:
print("\nInvalid input for preferred system of measurement. \
Please try again!\n")
else:
print("\nInvalid input for region/race. Please try again!\n")