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