Problem 1 โ€” BMI Calculator

def bmi(weight, height):
    return weight / (height ** 2)

Problem 2 โ€” Grade Mapper

def grade(score):
    if score >= 80: return "A"
    if score >= 65: return "B"
    if score >= 50: return "C"
    return "F"

Problem 3 โ€” Count Words

def count_words(text):
    return len(text.split())

Problem 4 โ€” Prime Checker

def is_prime(n):
    if n < 2: return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0: return False
    return True

Problem 5 โ€” Multiple Return Values

def stats(lst):
    return min(lst), max(lst), sum(lst)/len(lst)

low, high, avg = stats([3, 9, 5, 7])

Problem 6 โ€” Default Parameters

def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

greet("Ming")             # Hello, Ming!
greet("Ming", "Welcome")  # Welcome, Ming!

Problem 7 โ€” Scope Trap

total = 0
def add(x):
    total = total + x    # UnboundLocalError

def add_fixed(x):
    global total
    total += x

Problem 8 โ€” Recursion

def factorial(n):
    return 1 if n <= 1 else n * factorial(n-1)

Practise this on PyForm โ€” free

PyForm runs Python in your browser with an AI tutor trained for HKDSE. No install, no credit card.

Open PyForm โ†’