Project: budget tracker
From: Sofia Rossi, Team Lead
"Can you build me a small budget tracker for March? The budget per category is in budget.json and the card transactions are in transactions.csv. Refunds show up as negative amounts, and someone booked a conference badge under a category that isn't in the budget at all.
Money has to add up to the cent. At the end I'd like one line per budget category, the most used first, like travel: spent 3274.70 of 3000.00 (109.2%)."
Done looks like: the printed report, plus a list of overspent categories and the categories with no budget.
This project is built on exact money. Amounts start as text in the files and become Decimal straight away, so totals and percentages are exact and round the way a finance team expects.
Each transaction becomes a small dataclass, which keeps the fields named and typed. The rest uses the dictionary patterns from earlier units: totals per key, a comparison with a budget, and keys that appear on one side only.
Percentages are rounded half up to one decimal with quantize, and the report is printed text, checked line by line.
from decimal import Decimal, ROUND_HALF_UP
spent, budget = Decimal("3274.70"), Decimal("3000.00")
pct = (spent / budget * 100).quantize(Decimal("0.1"), rounding=ROUND_HALF_UP)
f"spent {spent} of {budget} ({pct}%)"
Out: 'spent 3274.70 of 3000.00 (109.2%)'. Decimals print with the digits they hold.