Module 06 · Lesson 02
Project: Tip & Bill Splitter
Build a tip and bill splitter: read the bill, tip rate and number of people, work out the tip, total and fair shares in whole cents, and print a tidy receipt.
- About 8 minutes
- Builds on earlier lessons
- 12-question quiz at the end
What you will learn
- Work out a percentage
- Share in whole cents
- Print a tidy receipt
- Check before dividing
The splitter in four parts
Friends share a bill and a tip. The program works out a fair share for each and prints a tidy receipt.
Read
The bill, the tip rate as a percentage and how many people are paying.
Check
No negative bills and at least one person, before any maths happens.
Work it out
A 15 percent tip on 40 is
40 * 15 / 100, which is 6.Share fairly
Count in whole cents so no cent goes missing, and say who pays any extra one.
Splitting bills everywhere
The same maths shows up wherever people pay together.
- RestaurantsCard machines suggest tips of 10, 15 or 20 percent and split the total.
- Shared ridesA taxi fare is split between everyone in the car.
- Group presentsClassmates chip in for a gift, with someone covering the odd cent.
- Shopping appsTax and discounts are percentages worked out the same way.
A percentage in one line
This line turns a tip rate into money. Tap each part to see its job.
Part 1 of 5
answer:Stores the tip, ready to add to the bill.
Build the splitter in five steps
Build the project one step at a time. Each example runs on its own: change it, then press Run.
Example 1 of 61. Percentages
A percentage is a number out of 100.
Your turn: Work out a 20 percent tip on a bill of 63.50.
bill = 40
rate = 15
tip = bill * rate / 100
print("Tip:", tip)
print("Total:", bill + tip)Press Run to see what it prints.Watch a bill split fairly
Press Step to run one line at a time. Three friends share a 50.00 bill with a 10 percent tip.
Ready. Press Step to run the first line.
- bill = 50.00
- rate = 10
- people = 3
- total_cents = round(bill * (100 + rate))
- share = total_cents // people
- extra = total_cents % people
- print(f"Each pays {share / 100:.2f}")
- print(f"{extra} cent left for one person")
Output so far
Nothing printed yet.
What Python does
Python reads the program from the top. Nothing has run yet.
Names
None yet.
Quick guesses
Guess first, then read why. A wrong guess is where the learning happens. Nothing here is scored.
Question 1 of 6
1.A 15 percent tip on 80: what prints?
bill = 80
print(bill * 15 / 100)0 of 6 answered
Common money maths misconceptions
Each card is something beginners often believe. Tap it to see what Python really does.
Misconception 1 of 4
Notes
Everything above, in words. Open it when you want the full explanation.
Read the notesNotesOpenClose
Syntax
The general shape of the statements in this lesson.
round(bill * percent / 100, 2) # store whole cents
f"{amount:.2f}" # always two decimals
f"{label:<10}{amount:>8.2f}" # aligned receipt line
cents = round(total * 100) # money as an int
base, extra = cents // people, cents % people
if people < 1: ... # check before dividingPress Run to see what it prints.Worked examples
Read the problem, run the code, then read why it works.
Example 1 of 2
A complete receipt
Print a receipt for a bill of 86.50 with a 12% tip split between 3 people, with shares that add up exactly to the total.
def tip_amount(bill, percent):
return round(bill * percent / 100, 2)
def split_cents(total, people):
cents = round(total * 100)
base, extra = cents // people, cents % people
shares = []
for i in range(people):
shares.append(base + 1 if i < extra else base)
return shares
bill, percent, people = 86.50, 12, 3
tip = tip_amount(bill, percent)
total = round(bill + tip, 2)
print(f"{'Bill':<10}{bill:>8.2f}")
print(f"{'Tip':<10}{tip:>8.2f}")
print(f"{'Total':<10}{total:>8.2f}")
shares = split_cents(total, people)
for number, cents in enumerate(shares, start=1):
print(f"{'Person ' + str(number):<10}{cents / 100:>8.2f}")
print("Check:", sum(shares) / 100 == total)Press Run to see what it prints.Predict first
How it works(open after your guess)
Common pitfalls
Mistakes people make with this topic, and what to write instead.
Pitfall 1 of 3
Shares that lose a cent
Spot the bug
Can you see what is wrong? Run it to watch it go wrong, then open the fix.
total = 100.00 share = round(total / 3, 2) print(share * 3)
Press Run to see what it prints.Show what goes wrong and the fixWhat goes wrong and the fix
In the real world
Where this shows up in software people use every day.
- Travel
Splitting a holiday cottage
Four friends rent a cottage for 731.00 and want each payment to be as equal as possible while still adding up to the exact price the owner asked for.
Why Python fits
73,100 cents divides by 4 exactly, so nobody pays an extra cent. Change the price to 731.01 and the first friend would pay one cent more, with the total still exact.
Pythonprice = 731.00 friends = ["Ana", "Bo", "Cy", "Dee"] cents = round(price * 100) base, extra = cents // len(friends), cents % len(friends) for i, name in enumerate(friends): pay = base + (1 if i < extra else 0) print(f"{name:<4}{pay / 100:>8.2f}")OutputPress Run to see what it prints.
Key takeaways
Tick each one you could explain to a friend.
0 of 5
Common questions
Guess the answer first, then open it.
Why not just use floats for everything?
Who pays the extra cent?
What about other currencies?
Your notes
Sign in to keep private notes alongside this lesson.
Where to go next
The quiz and the Lab live on their own pages, so this lesson stays about learning.
Finished this lesson?
Create an ID or sign in to save your progress across devices.
Check what you learned
12 questions, with an explanation after every answer. A verified attempt counts towards opening the module Lab.
Keep going in the Lab
7 tasks on this lesson, from writing code to tracing it by hand. Practice only, no XP.
Spaced review
Questions you miss in a quiz come back on a spaced schedule, so what you learned here sticks.
This lesson at a glance
The 6 lines of code this lesson uses, each with a short note. Keep it open while you do the quiz and the Lab.
What else you can search for and explore
Curious to go further? Each card opens a web search in a new tab.
References
Look things up here whenever you need the exact details.