Paul's Python CoursePPC

Project 13 of 13, for Yahya

Expense Tracker

The Act I capstone: a menu-driven expense tracker, built from a list of dictionaries and functions.

About 3 hours, in 12 steps, free and with no account needed.

Start the project

No account needed. Your work is kept in this browser until you sign up.

The brief

  • YahyaEveryone's paid into the end-of-term trip fund, and now the money's starting to go out.
  • YahyaThe receipts are spread over five pockets and two group chats, and honestly it's a mess.
  • YahyaI want one list and then totals by category, so the numbers tell us where it went before we book the coach.
  • CaitlinOoh, can it have a chart?
  • PaulThis is the biggest job yet, so plan it before you build it. The chart can wait, Caitlin.

Requirements, from Yahya. Aaesha numbered them:

  1. Ask 1 add, 2 list, 3 totals, 4 biggest, 5 quit: over and over, until the choice is 5.
  2. Add asks What for? , Category? and Amount? , then prints Added snacks. Each expense is a dictionary with an item, a category and an amount (a number), kept in a list in the order they were added.
  3. Tidy what's typed: no spaces at the ends of anything, and categories in lower case, so Travel and travel are one category.
  4. An amount that isn't a number, or isn't more than 0, gets That's not an amount. and nothing is added.
  5. List prints a line for each expense, in the order they were added: snacks: 18.40 (food).
  6. Totals prints a line for each category, alphabetical, food: 42.90, and then Total: 102.90.
  7. Biggest prints Biggest: coach hire, 212.00 (travel). If two tie, the one added first.
  8. With no expenses yet, list, totals and biggest each print No expenses yet.
  9. Quit prints 3 expenses, 102.90 in all. (1 expense for one) and stops.
  10. Any other choice gets Choose 1 to 5.

Two functions are checked on their own. category_totals(expenses) returns a dictionary of each category's total. biggest(expenses) returns the dictionary of the biggest expense, or None if the list is empty.

What it prints
1 add, 2 list, 3 totals, 4 biggest, 5 quit: 1
What for? snacks
Category? Food
Amount? 18.40
Added snacks.
1 add, 2 list, 3 totals, 4 biggest, 5 quit: 1
What for? coach hire
Category? travel
Amount? 212
Added coach hire.
1 add, 2 list, 3 totals, 4 biggest, 5 quit: 1
What for? sandwiches
Category? food
Amount? twenty
That's not an amount.
1 add, 2 list, 3 totals, 4 biggest, 5 quit: 2
snacks: 18.40 (food)
coach hire: 212.00 (travel)
1 add, 2 list, 3 totals, 4 biggest, 5 quit: 3
food: 18.40
travel: 212.00
Total: 230.40
1 add, 2 list, 3 totals, 4 biggest, 5 quit: 4
Biggest: coach hire, 212.00 (travel)
1 add, 2 list, 3 totals, 4 biggest, 5 quit: 5
2 expenses, 230.40 in all.

The lesson, step by step

  1. How Paul would break it down

    • PaulA big job is just small jobs in a row, and it helps to write the row down first.

    Before any code, Paul writes the steps on the chalkboard:

    1. Decide what the program remembers. One list, expenses, with a dictionary for each receipt.
    2. Add one expense. Ask, tidy, try the amount, and only then append a dictionary.
    3. The menu loop. Ask, branch, go round again. Leave on 5.
    4. One function per job. Totals by category, and the biggest expense, each return an answer. Listing and the menu print.
    5. The empty case. Every option has to cope with no expenses at all.
    6. Try to break it. twenty, 0, -5, a 7, Travel and travel. Yahya will.

    The next few steps show the three things you haven't built before: a list of dictionaries, a total for each category, and finding the biggest.

  2. A list of records

    A list can hold dictionaries. Each receipt is one dictionary, a record with the same three keys, and the list keeps the records in the order they came in.

    Riya's contact book looked records up by name. The trip fund doesn't need names: it needs every receipt, in order.

    .append() adds a new record on the end. A for loop over the list hands you one whole dictionary each pass, and expense['item'] reads inside it. The example prints a line for each of the three receipts, like snacks: 18.40 (food).

    The usual slip is to read expense as a position, 0, 1, 2. It's the dictionary itself.

  3. Receipts in a list

    Two receipts for the trip fund, added up.

  4. A total for each kind

    To keep a total for each kind of thing, use a dictionary with one key per kind, and add to that key's value each time round a loop.

    The trip fund needs a total for each category, and nobody knows in advance which categories will turn up.

    The first time a drink appears, it isn't a key yet. counts.get(drink, 0) gives 0 for it, and adding 1 makes the key. After that, .get gives the count so far. sorted(counts) gives the keys in alphabetical order. The example prints the dictionary, then coffee 1 and tea 3.

    The usual slip is counts[drink] = counts[drink] + 1. It crashes with a KeyError on the first of every drink.

  5. Adding to a category

    Food has a total already. Travel doesn't, yet.

  6. The best so far

    To find the biggest or smallest item in a list, start with the first one as the best so far. Then go through the list and swap in any item that beats it.

    Yahya wants the biggest expense. Neil wants his fastest lap. It's the same loop.

    Here the fastest lap is the smallest time. best starts at 62.4. Then 59.8 beats it, so best becomes 59.8. 61.0 doesn't beat that, and the second 59.8 only ties, so the first one stays. The example prints Best lap: 59.8.

    The usual slip is starting best at 0. Nothing is faster than 0, so it never changes. Start from the first item, after checking the list isn't empty: an empty list has no [0].

  7. Top score

    Three scores from the arcade cabinet.

  8. A menu that remembers

    • PaulThis is Neil's office-hours log, who came and for how long. The trip fund's the same shape, just with money.

    Neil's office-hours log has the trip fund's shape: a list of records, a function that adds one, a function that totals them, and a menu.

    return on its own, on line 8, leaves add_visit early, so a bad number never reaches the list. hours_by_student totals with .get(name, 0), like the drinks count, and sorted(totals) puts the names in alphabetical order.

    add_visit is given visits, so it appends to the one list main() keeps. The labels beside the code are the steps Paul took.

  9. Make the expense tracker

    Build the expense tracker to Yahya's ten requirements in the brief. Paul's chalkboard is the plan. The file is empty apart from Yahya's note.

    1 add, 2 list, 3 totals, 4 biggest, 5 quit: 3
    food: 18.40
    travel: 212.00
    Total: 230.40
    

    category_totals and biggest are checked on their own, with lists of receipts the checks make up. They return their answers, and the menu does the printing. category_totals works like the drinks count, and biggest like Neil's laps.

    The checks then type whole afternoons at it: receipts added, refused, listed and totalled, and wrong numbers. When one fails, read its inputs. That's the afternoon Yahya had. Run it yourself first and try to break it.

  10. Paul reads it

    • PaulIt works, and now I'm going to read it through properly. That's a different job altogether.

    Paul reads every program twice: once to see that it works, and once to see whether the next person could mend it. His list for this one:

    • Honest names. expenses and expense, not l and e. A category is category everywhere.
    • One job per function. If main() is longer than the rest put together, something in it wants its own name.
    • Nothing written twice. Checking for no expenses happens in three options. A small function, or one check before the branches, does it once.
    • Comments say why. The one above the > earns its place. # loop through wouldn't.
    • Read it aloud. A line that is hard to say is hard to read.

    Go through yours against the list. Change what you'd change, then check it again.

  11. Ship it

    • YahyaFood's forty-two ninety and travel's two seventy-two. I did it on paper as well and got the same both times.
    • YahyaAnd there's the story in the numbers, the coach is nearly all of it. One cheaper quote and we're fine.
    • PaulAnd that's the whole Workshop floor done! The clock still says 3:14, mind.