Project 25: Unit Converter Booth 📏¶
Difficulty: 🟢 Beginner
Run cells top to bottom. Each step builds on the previous one and saves real files under outputs/unit_converter_booth/.
Description¶
Build a tiny converter booth for km↔mi, C↔F, and kg↔lb — then export a handy cheat sheet.
Libraries & Modules¶
- Built-in math only
Python Concepts You'll Practice¶
Functions, formatting numbers, dictionaries of converters
🎛️ Parameter Variations¶
- Input values in
JOBS - Add your own converter functions
In [1]:
# Install dependencies for this project (safe to re-run)
import sys
!{sys.executable} -m pip install pillow numpy matplotlib wordcloud qrcode[pil] python-barcode fpdf2 jinja2 folium plotly pandas scipy ipywidgets -q
Skipping pip install (already installed)
In [2]:
# Interactive Jupyter setup — run this cell first
%matplotlib inline
from pathlib import Path
from IPython.display import display, Image as IPImage, HTML, Markdown, Audio, IFrame
OUTPUT_DIR = Path("outputs") / "unit_converter_booth"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"✅ Outputs folder: {OUTPUT_DIR.resolve()}")
✅ Outputs folder: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\unit_converter_booth
Step 1 — Define converters¶
In [3]:
# 🎛️ TWEAK THESE
JOBS = [
("km_to_mi", 10),
("mi_to_km", 6.2),
("c_to_f", 22),
("f_to_c", 72),
("kg_to_lb", 70),
("lb_to_kg", 154),
]
def km_to_mi(x): return x * 0.621371
def mi_to_km(x): return x / 0.621371
def c_to_f(x): return x * 9 / 5 + 32
def f_to_c(x): return (x - 32) * 5 / 9
def kg_to_lb(x): return x * 2.20462
def lb_to_kg(x): return x / 2.20462
FUNCS = {
"km_to_mi": (km_to_mi, "km", "mi"),
"mi_to_km": (mi_to_km, "mi", "km"),
"c_to_f": (c_to_f, "C", "F"),
"f_to_c": (f_to_c, "F", "C"),
"kg_to_lb": (kg_to_lb, "kg", "lb"),
"lb_to_kg": (lb_to_kg, "lb", "kg"),
}
print("Converters ready:", ", ".join(FUNCS))
Converters ready: km_to_mi, mi_to_km, c_to_f, f_to_c, kg_to_lb, lb_to_kg
Step 2 — Run jobs + save cheat sheet¶
In [4]:
lines = ["CodeItAll Unit Converter Booth", "-" * 34]
for name, value in JOBS:
fn, src, dst = FUNCS[name]
result = fn(value)
line = f"{value:.2f} {src} → {result:.2f} {dst}"
print(line)
lines.append(line)
out = OUTPUT_DIR / "unit_cheatsheet.txt"
out.write_text("\n".join(lines), encoding="utf-8")
print(f"Saved → {out}")
10.00 km → 6.21 mi 6.20 mi → 9.98 km 22.00 C → 71.60 F 72.00 F → 22.22 C 70.00 kg → 154.32 lb 154.00 lb → 69.85 kg Saved → outputs\unit_converter_booth\unit_cheatsheet.txt