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.

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.
- Discount comes off the subtotal first.
SAVE10→ 10% off,STUDENT→ 15% off,HALF→ 50% off, anything else → 0%. - Tip is worked out on the discounted subtotal. You tip on what you actually pay.
- Total = discounted subtotal + tip.
- Split the total between
people. Ifpeopleis 0,per_personis0.0(never crash).
Round every money value to 2 decimals.
The most common slip is doing step 2 before step 1:
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 codeCheck 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
- 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 maths.
- 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:
- 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 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.