If you want to have a better understanding of whatever you are learning, or improve your skills as a python developer, then you must build projects. That's why in this tutorial, we're going to be building a BMI calculator in python.
What is a BMI Calculator
BMI stands for Body Mass Index. It is a mathematical method invented by a Belgian statistician by the name of Lambert Adolphe Jacques Quetelet. The purpose of the BMI is to find out the status of a person's weight.
Here's a summary of what that status can be:
Body Mass Index (BMI) | Weight Status |
---|---|
Below 18.5 | Underweight |
18.5 - 24.9 | Normal |
25.0 - 29.9 | Overweight |
30.0 plus | Obese |
So a BMI calculator in this case, is essentially a python program that can be used to evaluate the status of a person's weight.
Here's the formula/algorithm for the BMI:
BMI = weight/heightยฒ
As you can see from the formula, the BMI is the ratio of a person's weight to the square of their height. The weight is measured in kilograms (kg) and the height in metres (m).
So your mission (if you wish to accept), is to create a python program, that can be used to easily calculate body mass index of your users, using the formula stated above.
Let's begin.
Collect User's Data
The data we need from the user are the user's weight and height. We're going to use the input
function in python to collect these data.
Here's how:
weight = input("please enter your weight (kg): ")
height = input("please enter your height (m): ")
But there's a little problem with the above code. The problem lies in the fact that the input
function returns a string. Though it is generally not a problem, in our code, we need the user's input to be of type float
. We cannot run our mathematical operations on strings, and as such, we need to convert them.
Here's how:
weight = float(input("please enter your weight (kg): "))
height = float(input("please enter your height (m): "))
Calculate the BMI
Now we've successfully gotten the user's weight and height, we can now proceed to calculating the BMI and printing it to the user.
Here's how you do it:
BMI = weight/height**2
print(f"BMI: {BMI}")
The **2
is how we square numbers in python.
If you are unfamiliar with the syntax we used for our print statement, they are called f-Strings in python. You can read about them here.
Determine the Weight Status
So the final thing we need to do, is to determine the weight status of the user and show them the result. In order to do this, we're going to use the if
, elif
and else
statements in python.
Here's the code:
if BMI < 18.5:
print("Your BMI falls within the underweight range")
elif (BMI > 18.5) and (BMI <= 24.9):
print("Your BMI falls within the normal or healthy weight range")
elif (BMI >= 25) and (BMI <= 29.9 ):
print("Your BMI falls within the overweight range")
else:
print("Your BMI falls within the obese range")
If you are not so familiar with conditional statements in python, you can read about Python Conditions and If statements on w3schools.
Entire Code
weight = float(input("please enter your weight (kg): "))
height = float(input("please enter your height (m): "))
BMI = weight/height**2
print(f"BMI: {BMI}")
if BMI < 18.5:
print("Your BMI falls within the underweight range")
elif (BMI > 18.5) and (BMI <= 24.9):
print("Your BMI falls within the normal or healthy weight range")
elif (BMI >= 25) and (BMI <= 29.9 ):
print("Your BMI falls within the overweight range")
else:
print("Your BMI falls within the obese range")
If you are also into javascript, you can check out my tutorial on how to build this same BMI calculator using javascript.
Top comments (0)