Skip to content
skillbld
Day 14 of 14 45 minPlanningBuildingTesting

Put two weeks of Python together. Plan, build and test a small expense tracker that uses functions, classes, dictionaries, files, errors and comprehensions.

By the end of today you can

  • Break a project into small, testable pieces
  • Model data with a class and store it as JSON
  • Validate input with exceptions
  • Produce summaries with dictionaries and comprehensions
  • Test your code with assert

Congratulations, you've reached the last day! Today you'll build a complete small program, an expense tracker, using almost everything from the past two weeks. Run each step as you go: every block builds on the previous ones.

The plan

Before writing code, break the problem into pieces:

Sketch
Plan of the expense tracker: an Expense class stored in a Tracker class. The tracker has add() with validation, save() and load() with a JSON file, and reports such as totals, top 3 and by month. Tests with assert check all three.
Mind map · click a dot to fold, drag to pan
SkillWhere it's used today
Functions (Day 9)Reports and helpers
Modules (Day 10)json, datetime, collections
Files & errors (Day 11)Saving, loading, validation
Classes (Day 12)Expense and Tracker
Pythonic code (Day 13)Comprehensions, sorted(key=…)

Step 1: the Expense class

Each expense is a small object that validates itself when it's created:

main.py · editable
Output
Press Run to execute this code.

@classmethod makes from_dict an alternative way to create an Expense from saved data: Expense.from_dict({...}).

Step 2: the Tracker

The tracker holds the list of expenses and knows how to add, report and save:

main.py · editable
Output
Press Run to execute this code.

Step 3: save, load and a report

Save the data, load it back as if the program restarted, and print a small report:

main.py · editable
Output
Press Run to execute this code.

Step 4: test it

Professional code comes with tests. assert stops with an AssertionError if something isn't as expected. If every assert passes, nothing interrupts and you see the final message:

main.py · editable
Output
Press Run to execute this code.

Where to go next

Mind map · click a dot to fold, drag to pan

Recap: your two weeks

  • Days 1–4: running code, variables and types, strings, operators.
  • Days 5–8: decisions, loops, lists and tuples, dictionaries and sets.
  • Days 9–11: functions, modules, files and error handling.
  • Days 12–14: classes, Pythonic style, and a complete project with tests.

You can now read, write and structure real Python programs. Well done! 🎉

Knowledge check

Test what you learned

4 questions · pick an answer to see if you're right

0/4
  1. 1

    Why build the tracker as several small functions and a class instead of one long script?

  2. 2

    What does assert total_by_category(...)["food"] == 23.5 do if the value is wrong?

  3. 3

    Why convert Expense objects to dictionaries before saving?

  4. 4

    Where is the best place to reject a negative amount?

Finished today's lesson?