Python BMI Calculator Program
weight_kg / height_m ** 2 and uses if/elif to name the category: below 18.5 underweight, below 25 normal, below 30 overweight, otherwise obese.Body Mass Index (BMI) is a simple number that relates weight to height. The program is small, but it practises everything a real application needs: reading input, converting numbers, applying a formula, rounding the result, making a decision with if/elif/else and validating bad values.
BMI is only a rough screening number. It does not measure body fat and says nothing about an individual's health, so treat the program as a coding exercise, not as medical advice.
| Input | Weight in kilograms and height in metres |
|---|---|
| Output | The BMI rounded to one decimal place and its category |
| Approach | Apply weight / height ** 2, then compare with the category limits |
| Time complexity | O(1) |
Approach: how to calculate BMI in Python step by step
- Read the weight and the height and convert both with
float(). - Check that both values are greater than zero.
- Calculate
bmi = weight / height ** 2. - Round it with
round(bmi, 1)(see round()). - Compare the value with the category limits from top to bottom and return the first matching label.
Because the checks run in order, each elif only needs the upper limit; the lower limit is implied by the earlier condition.
How the BMI calculator code works
height_m ** 2 squares the height. calculate_bmi() only does the maths and bmi_category() only decides the label, so each function does one job and can be tested alone (see functions). An f-string such as f"{bmi:.1f}" formats the number with one decimal place for display.
weight = 70
height = 1.75
bmi = weight / height ** 2
print(bmi)
print(round(bmi, 1))
print(f"{bmi:.1f}")BMI categories (WHO adult ranges)
| BMI | Category | Condition in code |
|---|---|---|
| Below 18.5 | Underweight | bmi < 18.5 |
| 18.5 to 24.9 | Normal weight | bmi < 25 |
| 25.0 to 29.9 | Overweight | bmi < 30 |
| 30.0 and above | Obese | else |
Variations: pounds and inches, and input validation
For imperial units the formula is 703 * weight_lb / height_in ** 2. A real program must also reject zero or negative numbers, because a height of 0 would cause a ZeroDivisionError and negative values give meaningless results. Children and teenagers use age-based percentile charts, not these adult limits.
Examples you can run
Press Run, then edit the code and run it again.
BMI and category for one person
70 kg at 1.75 m gives a BMI of 22.9, which is in the normal range.
def bmi_category(bmi):
if bmi < 18.5:
return "Underweight"
elif bmi < 25:
return "Normal weight"
elif bmi < 30:
return "Overweight"
return "Obese"
def calculate_bmi(weight_kg, height_m):
return weight_kg / (height_m ** 2)
bmi = calculate_bmi(70, 1.75)
print(f"BMI: {bmi:.1f}")
print("Category:", bmi_category(bmi))Expected outputBMI: 22.9
Category: Normal weight
Interactive BMI calculator
The user types weight and height; the program prints the result in one line.
def bmi_category(bmi):
if bmi < 18.5:
return "Underweight"
elif bmi < 25:
return "Normal weight"
elif bmi < 30:
return "Overweight"
return "Obese"
def calculate_bmi(weight_kg, height_m):
return weight_kg / (height_m ** 2)
weight = float(input("Weight (kg): "))
height = float(input("Height (m): "))
bmi = calculate_bmi(weight, height)
print(f"Your BMI is {bmi:.1f} ({bmi_category(bmi)})")Expected outputWeight (kg): 58
Height (m): 1.68
Your BMI is 20.5 (Normal weight)
Alternative: imperial units
The same idea with pounds and inches, multiplied by the conversion factor 703.
def bmi_category(bmi):
if bmi < 18.5:
return "Underweight"
elif bmi < 25:
return "Normal weight"
elif bmi < 30:
return "Overweight"
return "Obese"
def calculate_bmi(weight_kg, height_m):
return weight_kg / (height_m ** 2)
def calculate_bmi_imperial(weight_lb, height_in):
return 703 * weight_lb / height_in ** 2
bmi = calculate_bmi_imperial(180, 70)
print(round(bmi, 1))
print(bmi_category(bmi))Expected output25.8
Overweight
Edge case: invalid input
Zero or negative values are rejected before any division happens.
def bmi_category(bmi):
if bmi < 18.5:
return "Underweight"
elif bmi < 25:
return "Normal weight"
elif bmi < 30:
return "Overweight"
return "Obese"
def calculate_bmi(weight_kg, height_m):
return weight_kg / (height_m ** 2)
weight = float(input("Weight (kg): "))
height = float(input("Height (m): "))
if weight <= 0 or height <= 0:
print("Weight and height must be positive numbers")
else:
print(round(calculate_bmi(weight, height), 1))Expected outputWeight (kg): 65
Height (m): 0
Weight and height must be positive numbers
Common mistakes
Entering height in centimetres
✗ Wrong
weight = 70
height = 175
print(round(weight / height ** 2, 1))✓ Right
weight = 70
height_cm = 175
height = height_cm / 100
print(round(weight / height ** 2, 1))Convert centimetres to metres with height / 100, or ask for metres and say so in the prompt.
Writing the formula without the exponent
✗ Wrong
weight = 70
height = 1.75
print(round(weight / height * 2, 1))✓ Right
weight = 70
height = 1.75
print(round(weight / height ** 2, 1))Use the power operator **: height ** 2.
Python BMI Calculator Program: questions people ask
What is the BMI formula in Python?
bmi = weight_kg / height_m ** 2. For pounds and inches use 703 * weight_lb / height_in ** 2.
How do I round the BMI to one decimal place?
Use round(bmi, 1) for a number or an f-string such as f"{bmi:.1f}" for text, which keeps trailing zeros (for example 22.00).
What BMI ranges should my program use?
The WHO adult ranges: under 18.5 underweight, 18.5 to 24.9 normal, 25 to 29.9 overweight, and 30 or more obese. They do not apply to children.
Keep learning
Step-by-step lessons:
Python NumbersPython if, elif and elsePython Input and Output
Related:
Python Simple Calculator ProgramPython Tip Calculator Program (Split the Bill)Python round() FunctionPython float() Function
More in mini programs:
Python Number Guessing Game ProgramPython Simple Calculator ProgramPython Random Password Generator ProgramPython Rock Paper Scissors Game ProgramPython Tip Calculator Program (Split the Bill)Python Star Pattern Programs (Triangle, Pyramid, Diamond) See all →