Capstone — Bill Splitter & Tip Calculator 🧾
This is it. Every function you wrote was a piece of one real app. Now you'll
assemble the engine and watch the pre-built restaurant frontend (top-right, Live
App tab) come alive — it calls your compute_bill() on every keystroke.
What you're building
A function that takes the raw inputs from the check…
compute_bill(subtotal, tip_percent, people, discount_code)…and returns a dictionary of every number the receipt needs.
The business rules
Do them in this exact order — the order changes the totals:
- Discount comes off the subtotal first.
SAVE10→ 10% off,STUDENT→ 15% off,HALF→ 50% off, anything else → 0%. - Tip is calculated on the discounted subtotal (you tip on what you actually pay).
- Total = discounted subtotal + tip.
- Split the total between
people. Ifpeopleis 0, return0.0(don't crash).
Round every money value to 2 decimals.
The exact shape to return
The frontend reads these keys — return all nine:
{
"subtotal": 84, # the original
"discount_code": "SAVE10",
"discount_amount": 8.4, # dollars 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.30, # total / people
}A cleaner discount lookup
Instead of a long if/elif chain, a dict maps each code to its rate, and
.get(code, 0.0) returns 0.0 for unknown codes in one line:
DISCOUNTS = {"SAVE10": 0.10, "STUDENT": 0.15, "HALF": 0.50}
rate = DISCOUNTS.get(discount_code, 0.0) # 0.0 if the code isn't in the dict(This is a sneak peek at dictionaries — the star of Section 2.)
How to work
- Write
compute_billin the editor. - 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 math.
- Press ✓ Check to run the graded tests. Green across the board = Section 1 complete. 🎉
🎯 Your turn
Fill in the four TODOs in the starter so compute_bill(...) returns the full breakdown dict.
Do the steps in this order (the order changes the totals):
- Discount off the subtotal — SAVE10 10% · STUDENT 15% · HALF 50% · else none
- Tip on the discounted subtotal
- Total = discounted subtotal + tip
- Split the total between people (0 people →
per_personis0.0)
Round every money value to 2 decimals. Example: compute_bill(100, 20, 4, "SAVE10") → each person pays 27.0.
Hint — reuse the ideas from tip (L3), discount (L4) and split (L8).
Then press ▶ Run, play with the restaurant form in the Live App, and hit ✓ Check. All green = Section 1 complete. 🎉
