Mini-Project: Tip Calculator
Before you start
#What you'll build
A function-based tip calculator. The user enters a bill amount, a tip percent, and the number of people splitting the bill, and the program prints how much each person owes. The logic lives in clean, testable functions you can reuse from any other script. In this browser, type the three answers, one per line, into the "Input for this program" box before you press Run.
Sample interaction
Bill amount: 84.50
Tip percent (default 15): 18
Number of people (default 1): 3
Subtotal: $84.50
Tip (18%): $15.21
Total: $99.71
Per person: $33.24
#What you'll learn
After this lesson, you will be able to:
- Define a function with def and pass it parameters
- Return a value with return instead of printing it, so the caller decides what to do with it
- Give a parameter a default value, so the common case needs no argument
#Step 1: A bare tip function
Start with the simplest possible version: a function that takes a bill amount and a tip percent, and returns the tip amount in dollars.
def calculate_tip(bill, tip_percent):
return bill * tip_percent / 100
print(calculate_tip(100, 15)) # 15.0
print(calculate_tip(84.50, 18)) # 15.21return, not print. That means the caller decides what to do with the result. Second, the function is "pure" — given the same inputs, you get the same output, every time.What does this print? def tip(bill): bill * 0.15 result = tip(100) print(result)
What's the difference between `return x` and `print(x)` inside a function?
#Step 2: Default the tip percent
Pick a sensible default, 15%, so callers don't have to specify it every time.
def calculate_tip(bill, tip_percent=15):
return bill * tip_percent / 100
print(calculate_tip(100)) # 15.0 -- used the default
print(calculate_tip(100, 20)) # 20.0 -- overrode the defaultdef f(x=1, y) — Python will complain.What does this print? def share(total, people=1): return total / people print(share(60, 3)) print(share(3, 60))
#Step 3: Return the total, not just the tip
def calculate_check(bill, tip_percent=15):
tip = bill * tip_percent / 100
total = bill + tip
return tip, total
tip, total = calculate_check(84.50, 18)
print(f"Tip: ${tip:.2f}")
print(f"Total: ${total:.2f}")return a, b packs both values into a tuple. The caller unpacks them with tip, total = .... The :.2f format spec rounds to two decimal places — perfect for currency.What gets printed? def stats(): return 10, 20, 30 result = stats() print(type(result))
return 10, 20, 30, the comma syntax implicitly creates a TUPLE (not a list). If you assign to one variable, you get the whole tuple. If you assign to three variables (a, b, c = stats()), Python "unpacks" it. The parentheses around (10, 20, 30) are optional — it's the commas that make a tuple. This is why you can write a, b = b, a to swap two variables: the right side becomes a tuple, then the left side unpacks it.What does `return tip, total` actually return?
#Step 4: Split among N people
Add a third parameter for the number of people splitting the bill. Default it to 1 so non-split callers don't have to think about it.
def calculate_check(bill, tip_percent=15, people=1):
tip = bill * tip_percent / 100
total = bill + tip
per_person = total / people
return tip, total, per_person
tip, total, per_person = calculate_check(84.50, 18, 3)
print(f"Tip: ${tip:.2f}")
print(f"Total: ${total:.2f}")
print(f"Per person: ${per_person:.2f}")That's a real, useful function — a single line of input, three values back. Three callers can reuse it for solo dinners, dates, and group outings without changing anything.
#Now build it yourself
Tip + tax + split calculator
Write a function `calculate_total(bill, tip_percent=15, tax_percent=8, people=1)` that returns (tip, tax, total, per_person). Then write a small driver that asks the user for bill, tip_percent, and people (use the defaults if they press Enter on the latter two), and prints the receipt. Browser Python cannot pause for a prompt, so type the inputs, one per line, in the Input box above Run.
Bill amount: 84.50
Tip percent (default 15): 18
Number of people (default 1): 3
Subtotal: $84.50
Tax (8%): $6.76
Tip (18%): $15.21
Total: $106.47
Per person: $35.49This code calls input(). Python here can't pause to ask you, so type the answers below before running — one value per line, in the order the program asks for them.
#Now make it your own
Try one of these variations:
- Service-quality presets — ask the user to rate service (bad/ok/good) and map those to tip percents (10/15/20). Then call the function with the mapped value.
- Tip rounding. Round each person's share UP to the nearest dollar so the total never falls short. Use
math.ceil(). - Currency toggle. Accept a currency code (USD, EUR, INR) and print the right symbol. Use an if/elif chain to pick the symbol.