Building a BMI Calculator with Python

Building a BMI Calculator with Python

Introduction

A BMI calculator is a device that is used to determine the body mass index of an individual. It takes the weight and height of an individual and uses those two parameters to come up with a clinical conclusion as to whether they are a good weight for their height.

In this tutorial, we will build a BMI calculator using Python programming language.

This calculator will ask the user to input their weight in kilograms and their height in meters. It will go ahead to divide the heights in meters squared by the weight and come up with the user's BMI and a clinical conclusion based on the BMI.

Understanding BMI

Body Mass Index(BMI) is defined as the measurement of an individual's weight(kg) with respect to the height(m) of the individual. It has a mathematical formula:

BMI = (Weight in kilograms) divided by (Height in meters squared).

To ascertain the nutritional status of an individual using the BMI, the World Health Organization designed several categories. Individuals with BMI below 18.5, are classified as underweight. Individuals whose BMI falls under the range of 18.5 and 24.9 are classified as normal weight. Individuals whose BMI falls under the range of 25.0 and 29.9 are classified as pre-obese. Individuals whose BMI falls under the range of 30.0 and 34.9 their nutritional status is classified as obesity class I. Individuals whose BMI falls under the range of 35.0 and 39.9 their nutritional status is classified as obesity class II. For Individuals whose BMI is above 40, their nutritional status is classified as Obesity class III.

Setting Up for The Project

To efficiently carry out this project, you need the following

  1. A code editor. (For this project, I used the VSCode editor)

  2. Python installed (Mine is version 3)

  3. Basic knowledge of Python programming language

  4. Understanding how the BMI calculation works

Step-by-step Guide to Building a BMI Calculator with Python

Step one:

In your code editor, click on New file. In the box that appears, enter the name you want to give your file, and remember to add the ".py" extension to indicate that it is a Python file. Now click enter. Your file should open and you can begin your project. For this tutorial, we will be naming our file “BMI calculator.py”.

Step Two:

Ask the user to input their weight and height. To do this, use the input() function and assign the input to a variable. For this tutorial, we assigned the weight of the user to the weight variable and the height of the user to the height variable.

height = input("enter your height in m: ")
weight = input("enter your weight in kg: ")

Step Three:

Convert the weight to an integer and the height to a float. To do this, simply wrap the weight input in an int() function and the height input in a float() function as indicated below.

height = float(input("enter your height in m: "))
weight = int(input("enter your weight in kg: "))

Step Four

The next step is to define a function for calculating the BMI.

BMI = weight/height**2

BMI= (weight/height **2)

For this tutorial, we will be needing our BMI value as a round figure. To achieve that, we will need to use the round() function to the assigned BMI values as indicated below.

BMI = round(weight/height**2)

BMI= round(weight/height **2)

Step Five

Next, using the if-elif-else condition, print the various BMI categories as instructed by the World Health Organization.

  1. If the BMI is below 18.5, tell the user their BMI and also show “You are underweight”.

  2. If the BMI is between 18.5 and 24.9, tell the user their BMI and also show “Kudos! You have a normal weight”.

  3. If the BMI is between 25.0 and 29.9, tell the user their BMI and also show “You are pre Obese”.

  4. If the BMI is between 30.0 and 34.9, tell the user their BMI and also show “You have obesity class I”.

  5. If the BMI is between 35.0 and 39.9, tell the user their BMI and also show “You have Obesity class II”.

  6. If none of the above conditions are true, that means the user's BMI is above 40 so, tell the user their BMI and also show “You have Obesity class III”.

if BMI < 18.5:
  print(f"Your BMI is {BMI}, you are underweight.")
elif BMI >= 18.5 and BMI <= 24.9:  
    print(f"Your BMI is {BMI}. Kudos! You have a normal weight.")
elif BMI >= 25.0 and BMI <= 29.:
    print (f"Your BMI is {BMI}, you are Pre obese.")
elif BMI >= 30.0 and BMI <= 34.9:
    print (f"Your BMI is {BMI}, you have Obesity class I.")
elif BMI >= 35.0 and BMI <= 39.9:
    print (f"Your BMI is {BMI}, you have Obesity class II.")
else:
    print (f"Your BMI is {BMI}, you have Obesity class III")

Final Code

Here is the final code of this project. If you followed the steps listed above, your code should look like this.

code snippet for building a BMI calculator with python

Final Result

This is the final output gotten when you run the code. Your BMI is calculated and the clinical classification is printed.

BMI calculator code output

Conclusion

The BMI calculator is of high importance in the clinical and Python programming language makes it easy for anyone to know their BMI. This project is a beginner programming project and can be improved upon with a good design.

Anyone looking forward to learning the Python programming language can attempt this coding exercise.

References:

World Health Organization

Ask Python

What is BMI
Wikipedia