Python
Python

 

 

Introduction:

Body Mass Index (BMI) is a numerical value derived from an individual’s weight and height. It is widely used to determine if a person is underweight, normal weight, overweight, or obese. While BMI doesn’t directly measure body fat, it provides a simple and effective method to assess weight in relation to height.

Objective:

The objective of this program is to calculate the BMI of an individual based on their weight (in kilograms) and height (in meters). The program will display the BMI value and give an interpretation of the result according to the standard BMI categories.

Python Code

# BMI Calculator Program

def calculate_bmi(weight, height):
    """Function to calculate BMI based on weight and height."""
    bmi = weight / (height ** 2)
    return bmi

def interpret_bmi(bmi):
    """Function to interpret the BMI result."""
    if bmi < 18.5:
        return "Underweight"
    elif 18.5 <= bmi < 24.9:
        return "Normal weight"
    elif 25 <= bmi < 29.9:
        return "Overweight"
    else:
        return "Obesity"

# Input weight and height from the user
def main():
    try:
        weight = float(input("Enter weight in kilograms: "))
        height = float(input("Enter height in meters: "))
        
        if weight <= 0 or height <= 0:
            print("Please enter valid positive values for weight and height.")
        else:
            bmi = calculate_bmi(weight, height)
            category = interpret_bmi(bmi)
            
            print(f"\nYour BMI is: {bmi:.2f}")
            print(f"Category: {category}")
    
    except ValueError:
        print("Invalid input. Please enter numeric values for weight and height.")

# Run the main function
if __name__ == "__main__":
    main()

Program Explanation

This Python program calculates and categorizes the Body Mass Index (BMI) for an individual. Here’s how it works:

    1. calculate_bmi function: This function takes the weight (in kilograms) and height (in meters) as input and computes the BMI using the formula:
bmi = weight / (height ** 2)
  1. interpret_bmi function: This function categorizes the BMI result into one of the following categories:
    • Underweight: BMI less than 18.5
    • Normal weight: BMI between 18.5 and 24.9
    • Overweight: BMI between 25 and 29.9
    • Obesity: BMI 30 and above
  2. Main function: The program prompts the user to input their weight and height. It then calculates the BMI and provides an interpretation based on the BMI value.
  3. The program includes error handling to ensure that users enter valid numeric values for weight and height.

How to Run the Program

To run this program, follow these steps:

    1. Ensure that you have Python installed on your computer. You can download Python from python.org.
    2. Save the code above into a Python file, for example, bmi_calculator.py.
    3. Open a terminal or command prompt window.
    4. Navigate to the directory where the Python file is saved.
    5. Run the program by typing the following command:
python bmi_calculator.py
  1. The program will prompt you to enter your weight and height. After entering the values, it will display your BMI and category.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)