Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Tip Calculator

Add a percentage tip and split the total with Python. Students keep the tip, combined bill, and per-person amount distinct and check the divisor's valid domain.

BeginnerPhase 6 - Category 5: Real-World Logic ProblemsExample 10 of 15
tip-calculator.py
1# Take inputs
2bill = float(input("Enter bill amount: "))
3tip_percent = float(input("Enter tip percentage: "))
4people = int(input("Enter number of people: "))
5
6# Calculate
7tip_amount = (bill * tip_percent) / 100
8total = bill + tip_amount
9per_person = total / people
10
11print(f"Tip amount: {tip_amount:.2f}")
12print(f"Total bill: {total:.2f}")
13print(f"Per person: {per_person:.2f}")

Output

Enter bill amount: 100
Enter tip percentage: 15
Enter number of people: 4
Tip amount: 15.00
Total bill: 115.00
Per person: 28.75

In this one

  • Add before splitting
  • Use percentage arithmetic
  • Validate the number of people

How it works

  1. 1

    Calculate the tip from bill.

  2. 2

    Add it once to obtain total.

  3. 3

    Divide total by people and print the three amounts.

Edge cases

What if people is zero?

What if people is zero?

The per-person calculation raises ZeroDivisionError.

Is the tip applied after splitting?

Is the tip applied after splitting?

No. It is added to the total before the split.

What's going on

tip_amount is calculated from the original bill. total adds that tip once, and per_person divides the combined amount by people.

For a bill of one hundred, ten-percent tip, and two people, each share is fifty-five. Rounding is only for display; individual rounded shares may not sum exactly in real money. Zero people raises division by zero, and negative counts are not rejected here.

What if people is zero? The per-person calculation raises ZeroDivisionError.

Is the tip applied after splitting? No. It is added to the total before the split.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is split among people?

Pick an answer to check it.

Question 2

What if people is zero?

Pick an answer to check it.

Questions people ask

What if people is zero?

The per-person calculation raises ZeroDivisionError.

Is the tip applied after splitting?

No. It is added to the total before the split.

What is split among people?

Bill plus tip.