Calculate Body Mass Index (BMI) in Python

weight / height². Height in metres. 175 cm is 1.75, not 175 — that BMI looks fatal.

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")

Output

Enter your weight in kg: 68
Enter your height in meters: 1.75
Your BMI is: 22.2
Category: Normal weight

In this one

  • height is metres. 175 cm → 1.75
  • height ** 2, not height * 2

What's going on

BMI = weight / height². Height in metres. Type 175 because you think in cm and the BMI is ~0.002. The categories will call you underweight, which is funny until it isn't.

height ** 2 is height squared. height * 2 is not.

The cutoffs (18.5, 25, 30) are the WHO buckets. They are blunt. The code doesn't know your sport or your bones.

height <= 0 is required. Divide by zero is a crash, not a 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.