Lesson 8 of 8

Practical Graduation: CLI Expense Tracker

Consolidate everything: File I/O, JSON, Lists, and Functions. Build a real-world tool that you'll use as the template for every future AI data pipeline.

Step 1 · build

Requirement Checklist

To finish this module, your tracker must:

  1. Add an expense (amount, category, description).
  2. List all expenses with a clean format.
  3. Persist data to an expenses.json file.
  4. Handle errors (like a missing data file or invalid amount).
import json
from pathlib import Path

def save_expenses(expenses):
    with open("expenses.json", "w") as f:
        json.dump(expenses, f, indent=2)

def add_expense(amount, cat, desc):
    # The same dictionary pattern used in chat history
    expense = {"amount": float(amount), "category": cat, "desc": desc}
    # ... logic to load and append ...
    print(f"Added ${amount} for {cat}")