…
…
Read a three-month expense file, total the spending by category and month, find the categories over their budget limit and the most expensive purchases, calculate how many months remain until a savings goal, and save a summary report as JSON.
Alex is a second-year university student in Port Alder. A scholarship and a part-time job bring in $975 a month, but Alex struggles to make it to the end of the month. Since September, Alex has written down every expense on their phone and exported the three months of records as expenses.csv: 117 rows, each with a date, a category, an amount, and a description.
Outputdate,category,amount,description
2026-09-01,Rent,260.00,flat share
2026-09-01,Groceries,34.00,weekly groceries
...In this project you will write a small budget tool that answers Alex's questions: Where does my money go the most? Did the saving I started in October work? Which categories went over their limit? What are my most expensive purchases? How many months until I can afford a $1,500 laptop?
What you will have at the end: a report aligned by category and by month, the last month's limit overruns, and a budget_report.json file:
OutputOver the limit in November:
Entertainment $134.50 / $75 (+$59.50)
Café & meals $157.25 / $125 (+$32.25)
Clothing $85.75 / $75 (+$10.75)Lessons used: formatting (22), lists and sorting (23–24), accumulating in dictionaries and lists of records (26–28), tuples and sets (29), comprehensions (30), sorting with key and lambda (31), raising errors with raise (32), CSV and JSON files (34–35), math.ceil (36), datetime (37).
How to work: The steps share one session; the functions and variables you define in one step are used by the next steps, so run the steps in order. In each step, the comment lines tell you what to do; you write the parts that have ___. The check also tries your functions with other sample data, in addition to checking the output.
What you'll do
expenses.csvDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
For three months Alex has written down every expense in a spreadsheet on their phone and exported it as expenses.csv. Each row has a date, a category, an amount, and a description.
The first job is to read the file. csv.DictReader gives you each row as a dictionary whose keys are the column headers, but every value comes back as a string. You cannot add up "260.00", so you will convert the amount field to a float while you read. All the later steps use this expenses list.
This step's code cell is for members.Sign up free
Alex's first question: "Where does my money go the most?" To answer it you will find the total of each category. The accumulation pattern from lesson 27 fits this job exactly: an empty dictionary, a loop, and result[k] = result.get(k, 0) + amount.
Then sort the categories by total, largest first. If you loop over sorted(totals, key=totals.get, reverse=True), the keys come out ordered by their values (lesson 31). On each line also show the category's share of all spending: the
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
:.1%0.29529.5%This step's code cell is for members.Sign up free
Alex says they started tracking a budget in October. Did it work? To see it, you will find the total of each month and compare it with the month before.
The date is stored in the file as a string like "2026-10-14". datetime.strptime(text, "%Y-%m-%d") from lesson 37 turns the string into a date, and .month gives the month number. You read the month name from the MONTH_NAMES list (the empty string at the start makes index 1 January).
The rate of change is (this month - previous month) / previous month. If the rate is negative, spending went down and you add a ▼; otherwise you add a ▲. Use abs to show the percentage without its sign.
This step's code cell is for members.Sign up free
Alex has set a monthly upper limit for each category (LIMITS). You will find which categories went over the limit in the last month.
overruns(records, limits, month) must first select only the records of that month (a comprehension and month_number do the job), then use category_totals from step 2 to find the totals of those records. For each category over its limit, a (category, spent, limit) tuple goes into the list.
The list must be sorted by the overrun amount, largest first, not by the amount spent: spending $155 against a $150 limit is a smaller problem than spending $130 against a $75 limit. You reach the tuple's elements by index inside a lambda: t[1] - t[2].
This step's code cell is for members.Sign up free
Rent and bills are about the same every month, and Alex cannot change them. What Alex really wants to know is the five most expensive optional expenses.
First write a comprehension that leaves out the fixed costs (the categories in the FIXED set). Then sort the list by amount, largest first, and slice the first five. Without a key, dictionaries cannot be compared; you give the criterion with lambda e: e["amount"].
To show the date as month/day/year, we read it with strptime and write it with strftime("%m/%d/%Y"); that line is ready for you.
This step's code cell is for members.Sign up free
Alex is saving for a $1,500 laptop and has $425 so far. Their monthly income (scholarship and part-time job) is $975. If they keep their average spending, how many months until they reach the goal?
months_left(goal, savings, monthly_saving) must separate three cases:
1. If the savings have already reached the goal, return 0.
2. If there is no monthly saving (zero or negative), the goal can never be reached: raise a ValueError (lesson 32).
3. Otherwise divide the remaining amount by the monthly saving and round up: if you need 12.5 months, the money is still not enough at the end of month 12, so you wait 13 months. Use math.ceil for this (lesson 36).
This step's code cell is for members.Sign up free
In the last step, collect all the findings in one dictionary and save it to budget_report.json; Alex will open this file at the end of each month to check their progress.
When float amounts are added up, long values like 0.30000000000000004 can appear, as in the 0.1 + 0.2 example (lesson 3). They do not appear in this data, but they can in another month's file. So round the categories to two decimals with a dictionary comprehension ({k: round(v, 2) for k, v in totals.items()}). For json.dump, remember the two settings from lesson 35: ensure_ascii=False writes letters like the é in "Café" as they are, and indent=2 makes the file readable.
After saving, read the file back and print a few fields to see that it was saved correctly.
This step's code cell is for members.Sign up free