thecodex.expert · The Codex Family of Knowledge
Tier 0 · Absolute Beginner · Python Project

Tip Calculator

Work out the tip and total for a restaurant bill, split between any number of people. Real-world maths made simple.

🧠 Teaches how to think spoonfed, every age Last verified:

1 The Problem

We want a program that takes a bill amount and a tip percentage, works out the tip and the new total, and can split it between people. It is a genuinely useful tool, and it teaches percentages, division, and clean money formatting.

Where this shows up: any app dealing with money — checkout totals, tax, discounts, splitting costs, invoices. The pattern of “take a number, apply a percentage, format as money” is everywhere in commerce.

2 How to Think About It

Think about the maths before the code:

The plan — in plain English
1. Ask for the bill amount, the tip percent, and how many people. → 2. Tip = bill × (percent ÷ 100). → 3. Total = bill + tip. → 4. Per person = total ÷ people. → 5. Show it all, tidied to two decimal places like real money.

Ask bill, tip %, people

Tip = bill x percent/100

Total = bill + tip

Per person = total / people

Show tip, total, per person

3 The Build — explained part by part

Here is the complete tip calculator, explained line by line below.

Pythontip.py
# A tip calculator that also splits the bill.

bill = float(input("Bill amount: "))
tip_percent = float(input("Tip percent (e.g. 15): "))
people = int(input("Split between how many people? "))

# Work out the tip, then the total, then each person's share.
tip = bill * (tip_percent / 100)
total = bill + tip
per_person = total / people

# :.2f means "show two decimal places", like real money.
print(f"Tip: ${tip:.2f}")
print(f"Total: ${total:.2f}")
print(f"Each person pays: ${per_person:.2f}")
What each part does — in plain words
bill = float(input(...)) — read the bill as a decimal number (money has cents).

tip_percent = float(...) — read the tip percent, like 15.

people = int(...) — read how many people. This is a whole number (you cannot have half a person).

tip = bill * (tip_percent / 100) — turn the percent into a fraction (15 becomes 0.15) and multiply. The brackets make sure the division happens first.

total = bill + tip and per_person = total / people — simple addition and division.

{tip:.2f} — the :.2f formats the number with exactly two decimal places, so $3.5 shows as $3.50 like real money.
Common mistakes — and how to avoid them
✗ Writing bill * tip_percent / 100 without thinking about order — it happens to work, but bill * (tip_percent/100) makes the intent clear.
✓ Use brackets to show “turn the percent into a fraction first”.
✗ Forgetting :.2f — then $3.5 shows instead of $3.50, which looks wrong for money.
✓ Always format money with :.2f for two decimal places.
✗ Using int() for the bill — then $19.99 becomes $19.
✓ Use float() for money so cents are kept.

4 Test & Prove Each Part

We test the maths against amounts we can work out by hand — a $100 bill at 10% should give a $10 tip and a $110 total.

A 10% tip on $100 is exactly $10
The total is the bill plus the tip
Splitting between 2 people halves the total
A 0% tip leaves the total equal to the bill
Pythontest_tip.py
def calculate(bill, tip_percent, people):
    """Return (tip, total, per_person) for a bill."""
    tip = bill * (tip_percent / 100)
    total = bill + tip
    return tip, total, total / people


def test_ten_percent():
    """10% of $100 is $10."""
    tip, total, _ = calculate(100, 10, 1)
    assert tip == 10
    assert total == 110


def test_split_in_two():
    """Splitting $110 between 2 people is $55 each."""
    _, _, per = calculate(100, 10, 2)
    assert per == 55


def test_zero_tip():
    """A 0% tip means total equals the bill."""
    _, total, _ = calculate(50, 0, 1)
    assert total == 50

Run with pytest -v. We pulled the maths into a calculate function so tests can check it directly against hand-worked answers.

5 The Interface

INPUTbill, tip %, peopletwo decimals and a whole number
What it expects
Bill amount: 80
Tip percent: 15
Split between how many people? 4
OUTPUTtip, total, per personmoney, 2 decimals
What it returns
Tip: $12.00
Total: $92.00
Each person pays: $23.00

6 Run It & Automate It

Run it locally
python3 tip.py
Enter the bill, tip percent, and number of people.

Jenkins runs the tests automatically — each line explained below.

What you should see when it works
Terminala real run
Bill amount: 80
Tip percent (e.g. 15): 15
Split between how many people? 4
Tip: $12.00
Total: $92.00
Each person pays: $23.00
If it breaks — how to fix it
🚨 ZeroDivisionError.
You entered 0 people. There must be at least 1 person to split between.
🚨 ValueError on the bill.
You typed something that is not a number. Type digits like 49.99.
GroovyJenkinsfile
// Jenkinsfile — automated tests on every change.
pipeline {
    agent any
    stages {
        stage('Get the code') { steps { checkout scm } }
        stage('Install') { steps { sh 'python3 -m venv venv && . venv/bin/activate && pip install pytest' } }
        stage('Test') { steps { sh '. venv/bin/activate && pytest -v' } }
    }
    post {
        success { echo 'The maths checks out.' }
        failure { echo 'A calculation is wrong — look above.' }
    }
}
🎯 Try this next — make it yours
  1. Round up per person. So the tip is always covered. (Teaches: math.ceil.)
  2. Suggest tip levels. Show 10%, 15%, and 20% side by side. (Teaches: doing the maths three times.)
  3. Guard zero people. Ask again instead of crashing. (Teaches: input validation.)
What you learned
You learned percentages in code, the difference between float and int, and money formatting with :.2f — the toolkit for any app that handles money. Related: f-strings, Variables & Types.