Skip to content

Expense splitter

Dex Right. We just got back from a resupply stop and everyone paid for different stuff and now nobody agrees on who owes who. It always turns into a whole thing. Can you build something where we log who paid what and it tells us how to settle up? For the sake of crew morale 🙄

What you're building

Add an expense (or 'done' to settle up):
Who paid? Alice
How much? 45.00

Add an expense (or 'done' to settle up):
Who paid? Bob
How much? 30.00

Add an expense (or 'done' to settle up): done

Total: £75.00 split between 2 people (£37.50 each)
Alice is owed £7.50
Bob owes £7.50

What you'll need

  • Numbers and arithmetic — splitting the total, calculating balances
  • Dictionaries — tracking how much each person paid
  • Lists — storing each expense as you collect them
  • Control flow — the input loop that runs until the user types "done"
  • Functions — separating collection from calculation makes the logic clearer

Hints

Track what each person paid. A dictionary with names as keys and amounts as values makes it easy to accumulate totals per person.

Fair share is simple. Total amount divided by number of people. The difference between what someone paid and their fair share tells you their balance: positive means they're owed, negative means they owe.

Collect first, calculate after. Gather all expenses in the loop. Once the user types "done", do all the maths in one go.

Going further

Once the basics work:

  • More than two people. The logic is the same — calculate everyone's balance against the fair share. Show each person's result.
  • Itemised expenses. Let the user add a description for each expense, then print an itemised list before the settlement.
  • Who pays who. Rather than just showing balances, calculate the minimum set of transfers needed to settle everyone up.