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:
- Add an expense (amount, category, description).
- List all expenses with a clean format.
- Persist data to an
expenses.jsonfile. - 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}")