Python Tip Calculator Program (Split the Bill)
A tip calculator is a friendly first project with real-world value. You read numbers with input(), apply percentage maths, format the answers as money using f-strings and optionally split the bill between friends.
The maths is only two formulas, so most of the learning is about doing it carefully: converting text to numbers, handling a zero tip and showing exactly two decimal places.
| Input | Bill amount, tip percentage and number of people |
|---|---|
| Output | Tip, total bill and the amount each person pays |
| Approach | tip = bill * percent / 100, total = bill + tip, each = total / people |
| Time complexity | O(1) |
Approach: how to calculate a tip and split the bill
- Read the bill and convert it with
float(). - Read the tip percentage, for example
15for 15%. - Read the number of people and convert it with
int(). - Compute
tip = bill * percent / 100. - Compute
total = bill + tip. - Compute
each = total / peopleand print everything with two decimals.
A useful shortcut is total = bill * (1 + percent / 100), which gives the same total in one line.
How the tip calculator code works
The format specifier :.2f in an f-string rounds to two decimals and always shows both, so 5.5 prints as 5.50. The function returns three values as a tuple, which the caller unpacks into three variables (see unpacking). Keep calculations in the function and printing outside it, as in the other mini projects.
bill = 84.5
print(bill * 15 / 100)
print(f"{bill * 15 / 100:.2f}")
print(round(bill * 1.15, 2))Variations: Decimal for money and rounding up per person
Floating-point numbers cannot store every cent exactly (for example 0.1 + 0.2 is 0.30000000000000004), which is fine for a display-only calculator. Accounting software uses decimal.Decimal instead. If you want each person's share to cover the bill, round up with math.ceil(each * 100) / 100 rather than rounding to the nearest cent; see math and statistics.
Examples you can run
Press Run, then edit the code and run it again.
Tip, total and share per person
A bill of 84.50 with an 18% tip split between 4 people.
def split_bill(bill, percent, people):
tip = bill * percent / 100
total = bill + tip
return tip, total, total / people
tip, total, each = split_bill(84.50, 18, 4)
print(f"Tip: {tip:.2f}")
print(f"Total: {total:.2f}")
print(f"Each: {each:.2f}")Expected outputTip: 15.21
Total: 99.71
Each: 24.93
Interactive tip calculator
The user types three values. The prompts and typed answers are shown as in a terminal.
bill = float(input("Bill amount: "))
percent = float(input("Tip percentage: "))
people = int(input("Number of people: "))
tip = bill * percent / 100
total = bill + tip
print(f"Tip: {tip:.2f}")
print(f"Total: {total:.2f}")
print(f"Each person pays: {total / people:.2f}")Expected outputBill amount: 120
Tip percentage: 15
Number of people: 3
Tip: 18.00
Total: 138.00
Each person pays: 46.00
Alternative: exact money maths with Decimal
Decimal avoids binary floating-point error. Create decimals from strings, not floats.
from decimal import Decimal, ROUND_HALF_UP
bill = Decimal("84.50")
tip = bill * Decimal("18") / 100
total = (bill + tip).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
print(tip)
print(total)Expected output15.21
99.71
Edge case: no tip and one person
A 0% tip returns the original bill, and one person pays everything. A check for fewer than one person avoids dividing by zero.
bill = float(input("Bill: "))
percent = float(input("Tip %: "))
people = int(input("People: "))
if people < 1:
print("At least one person is required")
else:
total = bill * (1 + percent / 100)
print(f"Each pays {total / people:.2f}")Expected outputBill: 50
Tip %: 0
People: 1
Each pays 50.00
Common mistakes
Forgetting to divide the percentage by 100
✗ Wrong
bill = 80
percent = 15
print(bill * percent)✓ Right
bill = 80
percent = 15
print(bill * percent / 100)A percentage must be divided by 100: bill * percent / 100.
Using int() for the bill
✗ Wrong
bill = int("84.50")✓ Right
bill = float("84.50")
print(bill)Money has decimals, so use float() (or Decimal) for the bill and int() only for the number of people. See convert string to float.
Python Tip Calculator Program (Split the Bill): questions people ask
How do I calculate a tip in Python?
Multiply the bill by the tip percentage divided by 100: tip = bill * percent / 100. Add it to the bill for the total.
How do I print money with two decimal places in Python?
Use an f-string with :.2f, for example f"{total:.2f}". It rounds to two decimals and keeps trailing zeros.
Should I use float or Decimal for money?
Decimal is the correct choice for accounting because it is exact. float is fine for a small practice calculator as long as you format the output to two decimals.
Keep learning
Step-by-step lessons:
Python NumbersPython f-StringsPython Input and Output
Related:
Python BMI Calculator ProgramPython Simple Calculator ProgramPython round() FunctionPython float() Function
More in mini programs:
Python Number Guessing Game ProgramPython Simple Calculator ProgramPython Random Password Generator ProgramPython BMI Calculator ProgramPython Rock Paper Scissors Game ProgramPython Star Pattern Programs (Triangle, Pyramid, Diamond) See all →