Syllabus Lesson 26 of 239 · Functions
Functions

Mini-Project: Unit Converter

Time to combine everything. You will build a small toolkit of pure functions: each takes its input as a parameter, computes an answer, and returns it. No printing, no globals, no surprises. That is exactly what makes them easy to test and reuse, which is why the hidden tests can check them directly.

Every function here should carry both a docstring and type hints, the professional habits from the last lesson. Here is the shape to follow, shown with one converter:

def celsius_to_fahrenheit(c: float) -> float:
    """Convert a Celsius temperature to Fahrenheit."""
    return c * 9 / 5 + 32

You will write three more in the same style. The conversion formulas you need:

  • Celsius to Fahrenheit: multiply by 9, divide by 5, add 32.
  • Kilometres to miles: multiply by 0.621371.
  • Kilograms to pounds: multiply by 2.20462.
  • Hours to minutes: multiply by 60.

Because floats are not always exact, the tests compare with a small tolerance (using round), so do not worry about tiny decimal differences.

Your turn

Define four pure functions, each with a type hint on its parameter and return, and each with a one-line docstring. celsius_to_fahrenheit(c: float) -> float returns c * 9 / 5 + 32. km_to_miles(km: float) -> float returns km * 0.621371. kg_to_pounds(kg: float) -> float returns kg * 2.20462. hours_to_minutes(h: float) -> float returns h * 60. None of them should print.

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output