Back to teaching portfolio

Chapter 5 · Functions & Math Utilities

CSBP119 Practical Questions

Students modularize logic with parameterized helpers. Each problem emphasizes input validation, docstrings, and returning values rather than printing inside the helper.

Problem 1 · Health Metrics

BMI Calculator Utilities

Build a pair of functions: one computes BMI, the other classifies the result into WHO weight categories.

  1. Implement bmi(weight_kg, height_m) returning a float.
  2. Create classify_bmi(value) returning a string label.
  3. Call the helpers from a small script.
def bmi(weight_kg, height_m):
    """Return body mass index using metric units."""
    return weight_kg / (height_m ** 2)

def classify_bmi(value):
    if value < 18.5:
        return "Underweight"
    if value < 25:
        return "Normal"
    if value < 30:
        return "Overweight"
    return "Obese"

score = bmi(72, 1.78)
print(score, classify_bmi(score))
Problem 2 · Gradebook

Weighted Average Helper

Write a function that calculates a weighted course average by reading score/weight pairs from the user. The helper returns the normalized average without storing the entries.

  1. Prompt for the number of components.
  2. Loop through that many entries, accumulating sums.
  3. Return the computed value, don’t print from inside the helper.
def weighted_average():
    items = int(input("How many graded components? "))
    weighted_sum = 0.0
    total_weight = 0.0
    counter = 0
    while counter < items:
        score = float(input("Score for component: "))
        weight = float(input("Weight (e.g., 0.2): "))
        weighted_sum += score * weight
        total_weight += weight
        counter += 1
    if total_weight == 0:
        return 0.0
    return weighted_sum / total_weight

print("Course average:", round(weighted_average(), 2))
Problem 3 · Currency Helper

AED ↔ USD Converter

Implement a pair of functions to convert between AED and USD using a configurable rate.

  1. Create to_usd(amount_aed, rate) and to_aed(amount_usd, rate).
  2. Call the helpers with user-provided amounts.
  3. Display formatted output.
def to_usd(amount_aed, rate):
    return amount_aed / rate

def to_aed(amount_usd, rate):
    return amount_usd * rate

exchange_rate = 3.67
local_amount = float(input("AED amount: "))
foreign_amount = float(input("USD amount: "))

print("AED → USD:", round(to_usd(local_amount, exchange_rate), 2))
print("USD → AED:", round(to_aed(foreign_amount, exchange_rate), 2))