Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Calculate Body Mass Index (BMI) in Python

Square the height, check the units, then calculate BMI in Python. First-year practice covers safe division, ordered cutoffs, display rounding, and model limits.

BeginnerBasic Python ProgramsExample 16 of 25
calculate-body-mass-index-bmi.py
1# Program to calculate Body Mass Index (BMI)
2
3weight = float(input("Enter your weight in kg: "))
4height = float(input("Enter your height in meters: "))
5
6if height <= 0:
7 print("Height must be greater than zero.")
8else:
9 bmi = weight / (height ** 2)
10 print("Your BMI is:", round(bmi, 2))
11
12 if bmi < 18.5:
13 print("Category: Underweight")
14 elif bmi < 25:
15 print("Category: Normal weight")
16 elif bmi < 30:
17 print("Category: Overweight")
18 else:
19 print("Category: Obesity")

Sample Output

Category: Normal weight

The rounded BMI is 22.2, and the unrounded value lies below 25 but not below 18.5.

In this one

  • Square height with **
  • Validate a divisor before division
  • Apply ordered screening thresholds

How it works

  1. 1

    Read weight in kg and height in meters.

  2. 2

    Reject height <= 0; otherwise compute weight / height ** 2.

  3. 3

    Print the rounded BMI, then select a category using the original value.

Edge cases

Zero height

Enter height 0.

The guard prints an error and avoids division by zero.

Wrong unit

Enter 175 for a height measured in centimeters.

Convert it to 1.75 meters first; the program assumes the stated unit.

What's going on

BMI is weight divided by height squared. Height belongs in meters. Type 175 because you think in centimeters and the answer becomes tiny; the code cannot infer your units.

height ** 2 squares the height. height * 2 just doubles it and is the wrong formula. The outer check rejects zero and negative heights before division. With 68 kg and 1.75 m, the value is about 22.20.

The nested conditions walk adult screening thresholds from low to high. Once bmi < 25 matches, later categories are skipped. round(bmi, 2) changes the displayed value, but the comparisons use the unrounded bmi. These broad categories are not a diagnosis and do not apply unchanged to children. Weight validation is still missing, so a negative weight would give a meaningless category.

After this program

If the idea is still fuzzy, read Day 1: Introduction to Python in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 basic python programs, or see every topic on Python exercises.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why use height ** 2?

Pick an answer to check it.

Question 2

Are category tests based on the rounded display?

Pick an answer to check it.

Questions people ask

Why use height ** 2?

BMI divides by the square of height, not by twice the height.

Are category tests based on the rounded display?

No. The conditions compare the unrounded bmi variable.

Is this a medical diagnosis?

No. It is a coding example of broad adult screening categories, not personal medical advice.