Skip to the workspace
Pebellslearn by building
Fundamentals  ›  Capstone Project

Capstone — Bill Splitter & Tip Calculator

Fundamentals · Project 40 minA bill splitter turns a bill, tip and group size into a total, equal whole shares and any remainder.

A bill splitter turns a bill, tip and group size into a total, equal whole shares and any remainder.

Combine validation, arithmetic and functions into a receipt whose parts account for every rupee.

The individual bill functions now have to agree on a shared calculation. Build one numeric engine that applies discount, tip and splitting in the specified order, then returns every receipt field. A wrong intermediate value can affect several later fields, so trace dependencies as you assemble it.

Four stacked steps: the inputs 100, 20% tip, 4 people and SAVE10 flow into step 1, discount first (10% off 100 leaves 90); step 2, tip on what you pay (20% of 90, not 100, is 18); step 3, total (90 + 18 = 108); step 4, split with a guard for 0 people (108 ÷ 4 = 27.0). Each step writes its line into the returned dict: discount_amount, discounted_subtotal, tip_amount, total and per_person

What you're building

One function takes the inputs from the bill…

compute_bill(subtotal, tip_percent, people, discount_code)

…and returns a dict of every number the receipt needs.

Preparing this topic’s interactive investigation…

The business rules

Do them in this exact order. The order changes the totals.

  1. Discount comes off the subtotal first. SAVE10 → 10% off, STUDENT → 15% off, HALF → 50% off, anything else → 0%.
  2. Tip is worked out on the discounted subtotal. You tip on what you actually pay.
  3. Total = discounted subtotal + tip.
  4. Split the total between people. If people is 0, per_person is 0.0 (never crash).

Round every money value to 2 decimals.

The most common slip is doing step 2 before step 1:

What does this print?
subtotal, tip_percent = 100, 20
tip = subtotal * tip_percent / 100          # tip on the full bill
discounted = subtotal * 0.90                # SAVE10 applied afterwards
print(round(discounted + tip, 2))

Trace one value through the whole receipt

This combines functions, conditionals, arithmetic and dictionaries. The input subtotal is already numeric; parsing text is a separate boundary. The coupon selects a rate, the rate produces a discount amount, and that amount determines the base for the tip. Name those intermediate values so a wrong receipt can be traced to the first wrong step.

For 100, 20, 4, "SAVE10", the checkpoints are 10 discount, 90 discounted subtotal, 18 tip, 108 total and 27 share. If the output is 27.5, inspect the tip base: 20 instead of 18 explains the extra 2 divided among four people. The final symptom points upstream.

Treat the return dict as a small interface. Every key is needed even when its value is zero. Numbers remain numbers; formatting "$27.00" belongs to the receipt display. Returning strings too early prevents the next function from reusing the calculation.

The exact shape to return

The frontend reads these nine keys. Return all of them:

{
    "subtotal":            84,      # the original
    "discount_code":       "SAVE10",
    "discount_amount":     8.4,     # money taken off
    "discounted_subtotal": 75.6,    # subtotal - discount
    "tip_percent":         18,
    "tip_amount":          13.61,   # tip on the discounted amount
    "total":               89.21,   # discounted + tip
    "people":              4,
    "per_person":          22.3,    # total / people
}

A cleaner discount lookup

In the conditionals lesson you wrote the discount as an if / elif chain. A dict from the dictionaries lesson does it in one line: map each code to its rate, and .get(code, 0.0) gives 0.0 for any code that isn't there.

DISCOUNTS = {"SAVE10": 0.10, "STUDENT": 0.15, "HALF": 0.50}
rate = DISCOUNTS.get(discount_code, 0.0)   # 0.0 for an unknown code

Check relationships, not only examples

For the exercise's staged rounding policy, the discount amount and tip amount are rounded before the final total. Check that the discounted subtotal plus discount reconstructs the subtotal to the specified cent precision, and that discounted subtotal plus tip reconstructs the total. Using an unknown code should match using no discount; a zero-percent tip should leave the discounted subtotal unchanged.

Splitting has a separate limit: 10 / 3 rounded to cents gives 3.33, and three shares total 9.99. This lesson reports a common rounded share; it does not allocate the remaining cent. If exact allocation were required, represent the total as integer cents and distribute the remainder, for example 334, 333, 333. Decide who receives the extra cent before coding.

For an independent check, calculate the expected breakdown for 80, a 25% tip, two people and HALF, without opening the solution. Then change only the number of people: which fields should stay identical? Design one case where tip-on-original and tip-on-discounted produce different results. Passing the provided examples is evidence about those cases; explaining these relationships is stronger evidence that you understand the engine.

How to work

  1. Write compute_bill in the editor.
  2. Press ▶ Run to load it, then open the Live App tab and play with the restaurant form: change the tip, add people, pick a promo code. Your Python is doing the maths.
  3. Press ✓ Check to run the graded tests. Then use the independent checks above to verify your reasoning.

🎯 Your turn

Fill in the TODOs in the starter so compute_bill(...) returns the full breakdown dict. Do the steps in this order:

  1. Discount off the subtotal: SAVE10 10% · STUDENT 15% · HALF 50% · else none
  2. Tip on the discounted subtotal
  3. Total = discounted subtotal + tip
  4. Split the total between people (0 people → per_person is 0.0)

Round every money value to 2 decimals. Example: compute_bill(100, 20, 4, "SAVE10") → each person pays 27.0.

Hint: reuse your own pieces: the tip from operators (lesson 3), the discount from conditionals (lesson 5) and the safe split from exceptions (lesson 9).

Press ▶ Run, play with the restaurant form in the Live App, then ✓ Check. Explain the dependencies and try an unseen input before moving on.

Your task
Build compute_bill(subtotal, tip_percent, people, discount_code). It returns a dict with the full bill breakdown that powers the live receipt. Example: compute_bill(100, 20, 4, "SAVE10") → a dict where per_person is 27.0.
Project context and further resources
You're building a piece ofBill Splitter & Tip Calculator
This piece — compute_bill(): Ties every piece together to power the live receipt.
Scenario Full house: $100 of food, a 20% tip, split 4 ways, with SAVE10 applied. The engine returns every number the receipt shows.
compute_bill.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
compute_bill(subtotal, tip_percent, people, discount_code) → dictThe full bill breakdown the frontend renders as a receipt.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.