Phase 1: Defining Health Metric Calculations and Create Aggregated Function
This project aims to recreate popular health metric calculators found online, which help users determine their Body Mass Index (BMI), Maintenance Calories, Daily Protein Intake, and other health measurements. The project is structured in two phases:
Phase 1: Define Core Functionality
This phase focuses on developing the core functionality by creating custom Python functions for each health metric calculation. These functions will form the computational backbone of the application.
The following metrics will be defined:
Phase 2: Graphic User Interface Development
This phase involves building a user-friendly Graphic User Interface (GUI)
Firstly I created the Body Mass Index (BMI) function, This requires weight & height inputs. It's important to note that this code only takes weight in KG and height in CM, a future improvement will be to allow users to select between metric and imperial measures
def bmi(weight, height):
"""Calculate BMI using weight and height"""
bmi_value = weight / (height / 100) ** 2
return bmi_value
The next function I created was to calculate Basal Metabolic Rate (BMR) using weight, height, sex and age. We require sex as there is a different calculation for BMR depending on if the person is male or female and can be seen in the if statement below.
def bmr(weight, height, sex, age):
"""Calculate BMR using weight, height, sex and age"""
if sex == 'f':
bmr_value = (10 * weight) + (6.25 * height) - (5 * age) - 161
elif sex == 'm':
bmr_value = (10 * weight) + (6.25 * height) - (5 * age) + 5
else:
print("sex was not selected")
return None
return bmr_value
To calculate Maintenance Calories, we need inputs for weight, height, sex, age, and exercise level. This function builds upon the Basal Metabolic Rate (BMR) calculations and applies an activity multiplier based on exercise level. The multiplier reflects the fact that higher activity levels require more calories to maintain - the more exercise you do, the more calories your body needs to sustain that level of activity. This code takes the previously calculated BMR and multiplies it by the appropriate activity factor found in the IF statement to determine the total daily calories needed to maintain current weight.
def maintain_cal(weight, height, sex, age, exercise):
"""Calculate maintenance calories using weight, height, sex, age and exercise level"""
if sex == 'f':
bmr_value = (10 * weight) + (6.25 * height) - (5 * age) - 161
elif sex == 'm':
bmr_value = (10 * weight) + (6.25 * height) - (5 * age) + 5
else:
print("sex was not selected")
return None
if exercise == 1:
maintain_value = bmr_value * 1.2
elif exercise == 2:
maintain_value = bmr_value * 1.375
elif exercise == 3:
maintain_value = bmr_value * 1.55
elif exercise == 4:
maintain_value = bmr_value * 1.725
elif exercise == 5:
maintain_value = bmr_value * 1.9
else:
print("Exercise level was not selected")
return None
return maintain_value
The next three functions define the weight loss metrics: mild, moderate, and extreme. They build upon the same calculations used in the maintenance calories function and simply apply percentage reductions to the user's maintenance calories. Mild weight loss targets 90% of maintenance calories, moderate weight loss aims for 79%, and extreme weight loss reduces to 58% of maintenance calories.
def mild_weight_loss(weight, height, sex, age, exercise):
"""Calculate Mild Weight Loss Calories using weight, height, sex, age and exercise level"""
if sex == 'f':
bmr_value = (10 * weight) + (6.25 * height) - (5 * age) - 161
elif sex == 'm':
bmr_value = (10 * weight) + (6.25 * height) - (5 * age) + 5
else:
print("sex was not selected")
return None
if exercise == 1:
maintain_value = bmr_value * 1.2
elif exercise == 2:
maintain_value = bmr_value * 1.375
elif exercise == 3:
maintain_value = bmr_value * 1.55
elif exercise == 4:
maintain_value = bmr_value * 1.725
elif exercise == 5:
maintain_value = bmr_value * 1.9
else:
print("Exercise level was not selected")
return None
mild_weight_loss_value = maintain_value * 0.90
return mild_weight_loss_value
def moderate_weight_loss(weight, height, sex, age, exercise):
"""Calculate Moderate Weight Loss Calories using weight, height, sex, age and exercise level"""
if sex == 'f':
bmr_value = (10 * weight) + (6.25 * height) - (5 * age) - 161
elif sex == 'm':
bmr_value = (10 * weight) + (6.25 * height) - (5 * age) + 5
else:
print("sex was not selected")
return None
if exercise == 1:
maintain_value = bmr_value * 1.2
elif exercise == 2:
maintain_value = bmr_value * 1.375
elif exercise == 3:
maintain_value = bmr_value * 1.55
elif exercise == 4:
maintain_value = bmr_value * 1.725
elif exercise == 5:
maintain_value = bmr_value * 1.9
else:
print("Exercise level was not selected")
return None
moderate_weight_loss_value = maintain_value * 0.79
return moderate_weight_loss_value
def extreme_weight_loss(weight, height, sex, age, exercise):
"""Calculate Extreme Weight Loss Calories using weight, height, sex, age and exercise level"""
if sex == 'f':
bmr_value = (10 * weight) + (6.25 * height) - (5 * age) - 161
elif sex == 'm':
bmr_value = (10 * weight) + (6.25 * height) - (5 * age) + 5
else:
print("sex was not selected")
return None
if exercise == 1:
maintain_value = bmr_value * 1.2
elif exercise == 2:
maintain_value = bmr_value * 1.375
elif exercise == 3:
maintain_value = bmr_value * 1.55
elif exercise == 4:
maintain_value = bmr_value * 1.725
elif exercise == 5:
maintain_value = bmr_value * 1.9
else:
print("Exercise level was not selected")
return None
extreme_weight_loss_value = maintain_value * 0.58
return extreme_weight_loss_value
Example Text
def daily_protein(weight):
"""Calculate Recommended Daily Protein Intake using weight"""
protein_value = weight * 2.2
return protein_value
To create the final aggregated function for this project, we combine all the necessary inputs required by our individual calculation functions. The aggregated function collects user data (weight, height, sex, age, and exercise level) and passes these values to each specialized function. Results are rounded to make the metrics more readable in the final display. The output is a formatted string containing all health metrics, which will update dynamically when a user inputs different values.
def health_metric(weight, height, sex, age, exercise):
"""Include weight in kg, height in cm, age in years, exercise level as an integer from 1 to 5.
1 = Sedentary, 2 = Lightly active, 3 = Moderately active, 4 = Very active, 5 = Super active."""
health_bmi_value = bmi(weight, height)
health_bmr_value = bmr(weight, height, sex, age)
health_maintain_cal_value = maintain_cal(weight, height, sex, age, exercise)
health_mild_weight_loss_value = mild_weight_loss(weight, height, sex, age, exercise)
health_moderate_weight_loss_value = moderate_weight_loss(weight, height, sex, age, exercise)
health_extreme_weight_loss_value = extreme_weight_loss(weight, height, sex, age, exercise)
health_daily_protein_value = daily_protein(weight)
rounded_bmi_value = round(health_bmi_value, 2)
rounded_bmr_value = round(health_bmr_value, 2)
rounded_maintain_cal_value = round(health_maintain_cal_value, 2)
rounded_mild_weight_loss_value = round(health_mild_weight_loss_value, 2)
rounded_moderate_weight_loss_value = round(health_moderate_weight_loss_value, 2)
rounded_extreme_weight_loss_value = round(health_extreme_weight_loss_value, 2)
rounded_daily_protein_value = round(health_daily_protein_value, 2)
result_string = f"""BMI: {rounded_bmi_value}
BMR: {rounded_bmr_value} calories
Maintain Calories: {rounded_maintain_cal_value} calories
Mild Weight Loss Calories: {rounded_mild_weight_loss_value} calories
Moderate Weight Loss Calories: {rounded_moderate_weight_loss_value} calories
Extreme Weight Loss Calories: {rounded_extreme_weight_loss_value} calories
Daily Protein Intake: {rounded_daily_protein_value} g"""
print(result_string)
return result_string
Here is an example of the aggregated function, taking the weight, height, sex, age, and exercise level inputs with the result which is found in the python console
health_metric(57, 156, 'f', 24, 3)