DEV Community

Cover image for HOW TO BUILD A BMI (BODY MASS INDEX) CALCULATOR USING PYTHON
Ikanke-abasi Akpaso
Ikanke-abasi Akpaso

Posted on

HOW TO BUILD A BMI (BODY MASS INDEX) CALCULATOR USING PYTHON

The Body Mass Index (BMI) is a medical screening tool that uses your weight and height to estimate your body fat percentage and assess your health risk.
As a beginner in Python, just like me, a body mass index calculator is one of the simplest and useful projects you can create
Building a BMI calculator involves the following steps:

1.Set Up your code editor:

(I use Visual Studio Code) and open a new file (CTRL + N), save it using the name bmi_calculator.py (This is optional, you can call it whatever name you want, but this is preferable for easy referral)

2.Write a Comment:

You’ll want to add a comment on your code editor, which includes the mathematical formula for calculating BMI, as well as the different BMI categories.

3.Get User Input:

Create a weight variable and a height variable. Both variables will store the input function, input(), which allows for a user’s response. Inside the input function, you can use questions such as Insert your height or What is your weight? whatever your choice is

Height = float(input("What is your height?: "))
Weight = float(input("What is your weight in kg?: "))

The BMI calculation uses kg for weight and meters for height, so it’s better to specify those units in your input. E.g.: Instead of just asking “what’s your weight?”, ask “what’s your weight in kg?”
We use float() here to convert the user’s input to a float so python does not read the numbers as a string.

4.Calculate BMI:

Next, we create another variable for the BMI calculation. I called mine BMI. In this variable, you store the mathematical formula for calculating the BMI.
So we have: BMI = Weight/ (Height *2)
The / operator is the division symbol while the * operator is for multiplication.
Just like in a normal calculator, Division goes before multiplication, but the brackets are solved first before any other operations. In BMI calculation, the height is multiplied first by 2 and then divided by the weight which is the reason for including the bracket.

5.Show the Result:

After the calculation, you’ll want to create a response back to the individual to tell the value of their BMI and what category it falls under.
We can do this using a flow control statement, in this case the if statement. An if statement’s clause will execute if the statement’s condition is True. The clause is skipped if the condition is false. The if statement is followed by an elif (else if statement that provides another condition that is checked if the previous conditions are false), and finally an else statement (runs when both if and elif are false)

• Using the if statement, we have:
• if BMI < 18.5:
• print("Underweight")
• elif BMI >= 18.5 and BMI < 25:
• print("Normal")
• elif BMI >= 25 and BMI < 30:
• print("Overweight")
• else:
• print("Obese")


You can check out the complete code here:
https://gist.github.com/IkankeAkpaso/7d2b3e3ab30a03febc8950c7baaf9d78

Although on Gist Hub I used something called the “f” string, which I have not discussed here yet, doing it like in the example above is still fine.
That’s it! you’ve built your own BMI calculator! Thanks for reading!

Top comments (0)