Project: Tip and Bill Splitter
Build a bill splitter with functions, rounded money, and an aligned results table.
Stage 1: Calculate a tip
A bill splitter starts with a bill and a tip rate. Store money as numbers, then use an f-string with :.2f to always show two decimal places.
bill = 48.50
tip_rate = 0.18
tip = bill * tip_rate
print(f"Tip: ${tip:.2f}")
The tip is 18% of the bill. The .2f part of the f-string formats the result like money.
Stage 2: Put the calculation in a function
A function groups a reusable job under a name. This one calculates a rounded total after adding a tip.
def total_with_tip(bill, tip_rate):
tip = round(bill * tip_rate, 2)
return round(bill + tip, 2)
total = total_with_tip(48.50, 0.18)
print(f"Total: ${total:.2f}")
round(..., 2) keeps the stored money values at two decimal places. The function returns the final total to the caller.
Stage 3: Split the total
Divide the total by the number of people. A width in an f-string, such as >6, lines up a small table.
total = 57.23
people_counts = [1, 2, 3]
print(f"{'People':>6} {'Each pays':>10}")
for people in people_counts:
share = round(total / people, 2)
print(f"{people:>6} ${share:>9.2f}")
The values line up because each field has a chosen width. The share is rounded to two decimal places before it is shown.
Stage 4: Finish the bill splitter
Combine the function, money formatting, rounding, and table into the complete program.
def total_with_tip(bill, tip_rate):
tip = round(bill * tip_rate, 2)
return tip, round(bill + tip, 2)
bill = 48.50
tip_rate = 0.18
tip, total = total_with_tip(bill, tip_rate)
print("Bill Splitter")
print(f"Bill: ${bill:.2f}")
print(f"Tip ({tip_rate:.0%}): ${tip:.2f}")
print(f"Total: ${total:.2f}")
print()
print(f"{'People':>6} {'Each pays':>10}")
for people in [1, 2, 3, 4]:
share = round(total / people, 2)
print(f"{people:>6} ${share:>9.2f}")
The program shows the bill details first, then several ways to split the same total. You can change the bill, rate, or list of people counts.
Key points
- Functions package a calculation you can reuse.
- round(value, 2) rounds a number to two decimal places.
- An f-string can format money with :.2f.
- Divide the total by the number of people to find each share.
- Field widths in f-strings align table columns.
Worked example
Dinner for four cost 2400 taka; service charge is 10%. Compute the grand total and each person's share.
bill = 2400 service = bill * 10 / 100 total = bill + service share = round(total / 4, 2) print(total, share)
How it works, step by step
- Percentage = multiply by 10 / 100 — or simply * 0.10.
- Grand total adds the charge to the original.
- Divide by heads and round for clean taka amounts.
References & Further Reading
Related entries from PyLabs's own reference library.
Practice exercise
Not passed yetSet bill to 36.0, tip_rate to 0.15, and people to 3. Calculate rounded variables named total and share. Print exactly:
Total: $41.40
Each person pays: $13.80
# Set bill, tip_rate, and people # Calculate total and share, then print both values
Check yourself
Project: Tip and Bill Splitter quiz
10 questions. You get instant feedback per question, and the timer starts when you press the button.
Lesson incomplete
Not completedCreate an ID or sign in to save lesson completion across devices.