Python Series track

Project 4 · Practice Projects

Project 4 — Unit Converter

Convert between common units (length, weight, temperature) using a menu-driven CLI. Extend the conversion table with new units without changing the core logic.

Practice Projects1 min readFree lesson

Difficulty: Lv ★ | Prior Knowledge: Basics Week 3, 9

Goal

Length/Weight/Temperature Conversion CLI (Bidirectional + Multi-category)

Differences from Basic Assignment

  • Basics Week 2 = Unidirectional single conversion
  • Here = Menu type + Bidirectional + Modular separation

Requirements

  • Menu: 1. Length (cm↔inch) 2. Weight (kg↔lb) 3. Temperature (°C↔°F) 4. Exit
  • Conversion functions defined separately

Challenge Task

  • Add currency conversion (external API)
  • Free input format (5 km to mile)

Assignment

Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

solution.pyPython
"""Unit Converter — Multiple Categories + Bidirectional"""

def cm_to_inch(cm): return cm / 2.54
def inch_to_cm(inch): return inch * 2.54
def kg_to_lb(kg): return kg * 2.2046226218
def lb_to_kg(lb): return lb / 2.2046226218
def c_to_f(c): return c * 9 / 5 + 32
def f_to_c(f): return (f - 32) * 5 / 9

def menu_length():
    direction = input("1.cm→inch 2.inch→cm: ").strip()
    val = float(input("Value:"))
    if direction == "1":
        print(f"{val} cm = {cm_to_inch(val):.2f} inch")
    else:
        print(f"{val} inch = {inch_to_cm(val):.2f} cm")

def menu_weight():
    direction = input("1.kg→lb 2.lb→kg: ").strip()
    val = float(input("Value:"))
    if direction == "1":
        print(f"{val} kg = {kg_to_lb(val):.2f} lb")
    else:
        print(f"{val} lb = {lb_to_kg(val):.2f} kg")

def menu_temp():
    direction = input("1.°C→°F 2.°F→°C: ").strip()
    val = float(input("Value:"))
    if direction == "1":
        print(f"{val}°C = {c_to_f(val):.2f}°F")
    else:
        print(f"{val}°F = {f_to_c(val):.2f}°C")

def main():
    handlers = {"1": menu_length, "2": menu_weight, "3": menu_temp}
    while True:
        choice = input("[Menu] 1.Length 2.Weight 3.Temperature 4.Exit:").strip()
        if choice == "4":
            break
        if choice in handlers:
            handlers[choice]()

if __name__ == "__main__":
    main()

Learn this lesson with a mentor

Work through the Python Series track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now