Unit Converter CLI
Build a Python command-line converter for units of length, weight and temperature: run python convert.py 5 km mi and it prints 5 km = 3.107 mi, or run it without arguments to use an interactive mode that keeps asking for conversions until you quit. Invalid input, such as an unknown unit, text instead of a number, or converting kilograms to meters, gets a clear message instead of a crash. It's a great first Python project: you'll learn functions, dictionaries as lookup tables, the argparse module, loops and input, handling errors with try and except, formatting numbers, and writing tests with pytest.
- Estimate
- ~6h
- Steps
- 5
- Completed by
- 0
- Proposed by
- codeseed.app
argparse
Project roadmap
- 01
Set up the argument parser
~1hCreate convert.py and use argparse to accept three optional positional arguments: the value, the source unit and the target unit, plus --list to show supported units and --decimals for rounding. Print the parsed values for now. Done when: python convert.py 5 km mi prints the three values, and --help shows the usage.
- 02
Write the conversions
~1.5hCreate dictionaries mapping each length unit to its value in meters and each weight unit to its value in kilograms, and convert by going through the base unit. Temperatures need formulas, so write a function that converts through Celsius for C, F and K. Accept common spellings (km, kilometer and kilometers). Done when: 5 km to mi gives 3.107, and 100 C to F gives 212.
- 03
Handle bad input
~1hRaise and catch clear errors for unknown units (listing similar ones), non-numeric values, mixing categories (kg to m), and temperatures below absolute zero. Print friendly messages and exit with code 1, never showing a traceback. Done when: each kind of bad input prints a helpful message.
- 04
Add interactive mode
~1hWhen no arguments are given, loop asking for a conversion such as 5 km mi (or a value and units one at a time), print the result, and continue until the user types quit or presses Ctrl+D, handling bad input without leaving the loop. Done when: interactive mode can do several conversions in a row and exits cleanly.
- 05
Test and polish
~1.5hFormat results with a sensible number of decimals and unit labels. Write pytest tests for each category, round trips (converting and back gives the original), negative temperatures, unit spellings and every error case. Done when: pytest passes.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack