Stop 04 · FunctionsProjects · 1/1

Unit Converter Toolkit

Part of the Python Roadmap

In short

This project turns everything about functions into one tidy program: small single-purpose converters, a reusable input helper, a menu built with match, and docstrings throughout. It's the kind of clean structure real codebases are built from.

A menu-driven program with small functions that convert kilometres to miles, Celsius to Fahrenheit, kilograms to pounds and rupees to another currency at a rate the user enters. It practises clean function design.

The jaadu opens a red metal toolbox with neat slots holding a small weighing scale, a thermometer, a measuring tape and a coin purse.

Think of it like this

A neatly labelled toolbox

A good toolbox has one tool per job, each in its labelled slot: a spanner for nuts, a screwdriver for screws. You pick the right one, use it, put it back. Your converter is a toolbox too: one small function per conversion, and a menu that hands you the right tool.

Watch it happen

How the toolkit is organised

main() menu
ask_number()
Converters
km_to_miles
celsius_to_fahrenheit
kg_to_pounds
convert_rupees

1/4main() shows the menu in a loop and uses match to route each choice.

The big ideas

What you need to know

One job per function

Converters only calculate, ask_number only reads input, main only coordinates. Small functions are easy to test and reuse.

Validate input once

ask_number keeps asking until it gets a valid number, so the converters never see bad data.

match for menus

Each case calls the right converter. case _ catches anything else, and choosing 5 breaks out of the loop.

if __name__ == "__main__"

main() runs only when you execute the file directly. If another file imports your converters, the menu won't start.

See it in code

Walk through the code

python
1"""Unit Converter Toolkit: small, reusable conversion functions."""2 3 4def km_to_miles(km):5    """Convert kilometres to miles."""6    return km * 0.6213717 8 9def celsius_to_fahrenheit(celsius):10    """Convert degrees Celsius to degrees Fahrenheit."""11    return celsius * 9 / 5 + 3212 13 14def kg_to_pounds(kg):15    """Convert kilograms to pounds."""16    return kg * 2.2046217 18 19def convert_rupees(rupees, rate):20    """Convert rupees using rate = foreign currency units per ₹1."""21    return rupees * rate22 23 24def ask_number(prompt):25    """Ask until the user types a valid number, then return it as a float."""26    while True:27        text = input(prompt).strip()28        if text.replace(".", "", 1).isdigit():29            return float(text)30        print("Please enter a positive number.")31 32 33def main():34    while True:35        print("1) km to miles  2) °C to °F  3) kg to lb  4) ₹ to other  5) quit")36        match input("Choose 1-5: ").strip():37            case "1":38                km = ask_number("Kilometres: ")39                print(f"{km} km = {km_to_miles(km):.2f} miles")40            case "2":41                c = ask_number("Celsius: ")42                print(f"{c}°C = {celsius_to_fahrenheit(c):.1f}°F")43            case "3":44                kg = ask_number("Kilograms: ")45                print(f"{kg} kg = {kg_to_pounds(kg):.2f} lb")46            case "4":47                amount = ask_number("Amount in ₹: ")48                rate = ask_number("Foreign units per ₹1: ")49                print(f"₹{amount:,.2f} = {convert_rupees(amount, rate):,.2f}")50            case "5":51                print("Goodbye!")52                break53            case _:54                print("Please choose a number from 1 to 5.")55 56 57if __name__ == "__main__":58    main()

1/6A module docstring describes the whole file.

Save as converter.py and run it. The currency rate is typed in by the user, so it never goes out of date.

Save yourself the pain

Common mistakes

Oops

Putting input() and print() inside every converter.

Do this instead

Keep converters pure: take numbers, return numbers. Handle input and output in one place, like main().

Oops

Hard-coding a currency exchange rate that is wrong next month.

Do this instead

Ask the user for the rate, or later fetch it from an API. Never bake changing data into code.

Oops

Calling main() with no __name__ check, so importing the file starts the menu.

Do this instead

Wrap the call in if __name__ == "__main__": so the module can be reused safely.

Your mission

Extend the toolkit

~45 min

Quick check

Why do the converter functions return values instead of printing them?

Go deeper

  1. 1Write one function per conversion, each with a docstring and a return value.
  2. 2Keep input and printing in main() and pure calculations in the helper functions.
  3. 3Store the menu options in a dict that maps choices to functions.

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge