Code a Simple BMI Calculator Using Python

Ahmad Waleed
3 min readNov 22, 2023

Body Mass Index (BMI) is a widely used indicator to assess a person’s body weight in relation to their height. It’s a simple yet effective way to determine whether someone is underweight, normal weight, overweight, or obese. In this article, we’ll delve into creating a basic BMI calculator using Python, which can serve as a starting point for understanding calculations involving BMI.

Step 1: Understanding BMI Formula

BMI is calculated using the formula:

Step 2: Setting Up the Python Environment

To build our BMI calculator, we’ll use Python. Ensure you have Python installed on your system. Additionally, we’ll employ a code editor or IDE like Visual Studio Code, PyCharm, or Jupyter Notebook.

Step 3: Writing the Python Code

Let’s begin by defining a function that computes BMI based on user input.

def calculate_bmi(weight, height):
bmi = weight / (height ** 2)
return bmi

This function takes weight and height as arguments and returns the calculated BMI.

Next, let’s prompt the user to input their weight and height:

weight = float(input("Enter your weight in kilograms: "))
height = float(input("Enter your height in meters: "))

Then, call the `calculate_bmi` function with the provided values:

result = calculate_bmi(weight, height)
print("Your BMI is:", result)

Step 4: Adding Interpretation of BMI

Now that we have the BMI value, it’s essential to interpret it. Typically, BMI values fall into different 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 of 30 or greater

Let’s enhance our code to include these interpretations:

if result < 18.5:
print("You are underweight.")
elif 18.5 <= result <= 24.9:
print("You have a normal weight.")
elif 25 <= result <= 29.9:
print("You are overweight.")
else:
print("You are obese.")

Step 5: Testing the BMI Calculator

Execute the code in your Python environment. Input your weight and height to see the calculated BMI along with the interpretation.

Full Code

# Function to calculate BMI
def calculate_bmi(weight, height):
bmi = weight / (height ** 2)
return bmi

# User input for weight and height
weight = float(input("Enter your weight in kilograms: "))
height = float(input("Enter your height in meters: "))

# Calculate BMI using the function
result = calculate_bmi(weight, height)
print("Your BMI is:", result)

# Interpretation of BMI categories
if result < 18.5:
print("You are underweight.")
elif 18.5 <= result <= 24.9:
print("You have a normal weight.")
elif 25 <= result <= 29.9:
print("You are overweight.")
else:
print("You are obese.")

Conclusion:

Congratulations! You’ve successfully built a simple BMI calculator using Python. This basic implementation can be expanded further by incorporating error handling, creating a graphical user interface (GUI), or developing a web-based application. Understanding how to calculate and interpret BMI through programming lays a foundation for more complex health-related applications or analyses.

Thank you for reading until the end. Before you go:

  • Please consider clapping and following the writer! 👏
  • Follow us on Linkedin and Github.

--

--

Ahmad Waleed

I thrive at the intersection of data and discovery, wielding algorithms and statistical models to unlock insights that power informed decisions.