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.
1# Take inputs2bill = float(input("Enter bill amount: "))3tip_percent = float(input("Enter tip percentage: "))4people = int(input("Enter number of people: "))56# Calculate7tip_amount = (bill * tip_percent) / 1008total = bill + tip_amount9per_person = total / people1011print(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
Calculate the tip from bill.
- 2
Add it once to obtain total.
- 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.
๐Related Content
- ๐
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- ๐
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- ๐ป
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum