1 The Problem
We want a BMI calculator: take a person’s height and weight, compute their BMI, and tell them which category it falls into (underweight, normal, overweight). It teaches applying a formula and then categorising the result with ranges — a very common two-step pattern.
2 How to Think About It
Think about the two steps, before any code:
3 The Build — explained part by part
Here is the complete BMI calculator. Each line is explained below.
# Calculate BMI and interpret it.
height = float(input("Height in metres (e.g. 1.75): "))
weight = float(input("Weight in kilograms: "))
# BMI formula: weight divided by height squared.
bmi = weight / (height * height)
# Categorise the result using ranges.
if bmi < 18.5:
category = "underweight"
elif bmi < 25:
category = "normal weight"
else:
category = "overweight"
print(f"Your BMI is {bmi:.1f} ({category}).")
bmi = weight / (height * height) — the formula. The brackets ensure we square the height first, then divide.
height * height is height squared.if bmi < 18.5: ... elif bmi < 25: ... else: — categorise by range. The order matters: we check the lowest range first, so by the time we reach
elif bmi < 25 we already know it is at least 18.5.{bmi:.1f} — show the BMI to one decimal place, so 22.857 reads as a tidy 22.9.
weight / height * height without brackets — that divides then multiplies, giving the wrong answer.weight / (height * height).< 25 before < 18.5 mislabels everyone.elif can assume the earlier checks failed.4 Test & Prove Each Part
We test both the formula and the categorising against values we can verify by hand.
def calculate_bmi(weight, height):
return weight / (height * height)
def categorise(bmi):
if bmi < 18.5:
return "underweight"
if bmi < 25:
return "normal weight"
return "overweight"
def test_bmi_value():
"""68 kg at 1.75 m is about 22.2."""
assert round(calculate_bmi(68, 1.75), 1) == 22.2
def test_underweight():
"""A BMI of 17 is underweight."""
assert categorise(17) == "underweight"
def test_normal():
"""A BMI of 22 is normal."""
assert categorise(22) == "normal weight"
Run with pytest -v. Splitting into calculate_bmi and categorise lets us test the maths and the classification separately — cleaner and clearer.
5 The Interface
What it expects
Height in metres: 1.75
Weight in kilograms: 68What it returns
Your BMI is 22.2 (normal weight).6 Run It & Automate It
python3 bmi.pyEnter height and weight to see your BMI and category.
Jenkins runs the tests automatically — each line explained below.
Height in metres (e.g. 1.75): 1.75
Weight in kilograms: 68
Your BMI is 22.2 (normal weight).// Jenkinsfile — automated tests on every change.
pipeline {
agent any
stages {
stage('Get the code') { steps { checkout scm } } // download the code
stage('Install') { steps { sh 'python3 -m venv venv && . venv/bin/activate && pip install pytest' } } // test tool
stage('Test') { steps { sh '. venv/bin/activate && pytest -v' } } // run every test
}
post {
success { echo 'All tests passed.' }
failure { echo 'A test failed — look above.' }
}
}
- Imperial units. Accept pounds and feet/inches too. (Teaches: unit conversion.)
- Finer categories. Split overweight into more bands. (Teaches: more ranges.)
- Healthy weight range. Tell them the weight range for a normal BMI at their height. (Teaches: rearranging a formula.)