Skip to the content.

Python Backend • 7 min read

Home JS Frontend Python Backend boop
# Constants for the Harris-Benedict Equation
BMR_MALE = 88.362
BMR_FEMALE = 447.593
ACTIVITY_MULTIPLIERS = {
    "sedentary": 1.2,
    "lightly active": 1.375,
    "moderately active": 1.55,
    "very active": 1.725,
    "extra active": 1.9,
}

# Function to calculate BMR (Basal Metabolic Rate)
def calculate_bmr(age, gender, weight, height):
    if gender == "male":
        return BMR_MALE + (13.397 * weight) + (4.799 * height) - (5.677 * age)
    elif gender == "female":
        return BMR_FEMALE + (9.247 * weight) + (3.098 * height) - (4.330 * age)
    else:
        raise ValueError("Invalid gender")

# Function to calculate daily calorie requirements
def calculate_calories(age, gender, weight, height, activity_level):
    bmr = calculate_bmr(age, gender, weight, height)
    activity_multiplier = ACTIVITY_MULTIPLIERS.get(activity_level.lower(), 1.2)
    daily_calories = bmr * activity_multiplier
    return daily_calories

# Input values
age = int(input("Enter your age: "))
gender = input("Enter your gender (male/female): ")
weight = float(input("Enter your weight in kilograms: "))
height = float(input("Enter your height in centimeters: "))
activity_level = input("Enter your activity level (sedentary/lightly active/moderately active/very active/extra active): ")

# Calculate daily calorie requirements
calories = calculate_calories(age, gender, weight, height, activity_level)

# Display the result
print(f"Your daily calorie requirement is approximately {calories:.2f} calories.")

Your daily calorie requirement is approximately 2566.88 calories.

This code calculates and displays a person’s estimated daily calorie requirements using the Harris-Benedict Equation. Here’s how it works:

  1. Constants for Basal Metabolic Rate (BMR) for both males and females are defined at the beginning of the code. These constants are based on the Harris-Benedict Equation and represent the estimated number of calories a person’s body needs at rest.

  2. Two functions are defined: calculate_bmr(age, gender, weight, height) calculates the BMR based on the provided age, gender, weight, and height, while calculate_calories(age, gender, weight, height, activity_level) calculates the daily calorie requirements by adjusting the BMR according to the person’s activity level.

  3. The user is prompted to input their age, gender, weight in kilograms, height in centimeters, and activity level. The activity level is used to determine an activity multiplier from the predefined dictionary ACTIVITY_MULTIPLIERS.

  4. The calculate_calories function is called with the user’s input, and it calculates the daily calorie requirements by multiplying the BMR with the activity multiplier obtained from the dictionary.

  5. Finally, the code displays the estimated daily calorie requirement for the user based on their inputs, rounded to two decimal places.

In summary, this code takes user input for age, gender, weight, height, and activity level, uses the Harris-Benedict Equation to estimate the Basal Metabolic Rate (BMR), adjusts the BMR based on activity level, and then displays the estimated daily calorie requirement for the user’s maintenance.

It first collects information like age, gender, weight, height, and activity level from the user. Then, it uses a mathematical formula called the Harris-Benedict Equation to estimate their daily calorie requirement based on this information and displays the result in a simple message