Free lesson · Module 1, lesson 8 of 8

Mini app: a bill splitter

Build a bill splitter that asks for the bill, the tip percentage and the number of friends, and prints each share rounded to the rupee, testing it with known values.

The video for this lesson is being prepared

About 11 minutes, narrated in English, Hindi and Odia. The notes and the quiz below are open now.

Lesson notes

Plan first: input, processing, output

  • Input: the bill, the tip percent and the number of friends
  • Processing, in order: tip = bill * tip_percent / 100, then total = bill + tip, then share = total / friends
  • Output: each share, rounded to the nearest rupee with round

Which conversion for which box

  • input always hands back text, even when the person types digits
  • float for the bill and the tip percent (a bill can have paise, a tip can be 12.5%)
  • int for the number of friends (people come in whole numbers)
  • Dividing by text stops the program with TypeError on the line that does the division; the mistake is on the line where the text came in

The complete program

bill = float(input("Bill amount: "))
tip_percent = float(input("Tip percent: "))
friends = int(input("Number of friends: "))
tip = bill * tip_percent / 100
total = bill + tip
share = total / friends
print(f"Each friend pays ₹{round(share)}")

Test cases to work by hand first

  • ₹1,200, 10%, 4 friends: Each friend pays ₹330
  • ₹1,200, 0%, 4 friends: Each friend pays ₹300
  • ₹999, 5%, 3 friends: Each friend pays ₹350 (349.65 rounds up)
  • ₹700, 10%, 3 friends: Each friend pays ₹257 (256.67 rounds up)

The trap

  • "It ran without an error, so it is right": share = bill + tip / friends runs with no error but prints ₹1230 for ₹1,200, 10% and 4 friends, because / happens before +. Check the output against a case worked by hand.

Good to know

  • round sends an exact half to the even whole number: round(302.5) gives 302 and round(303.5) gives 304. Shares rarely land on exactly half a rupee.
  • Type 0 for friends and Python stops with ZeroDivisionError. Module 2 teaches the program to check first.

Try it yourself

  • Print the tip as well: add one line with an f-string after the tip is worked out. For ₹1,200 at 10% it should show ₹120.
  • Ask for the name of the restaurant with one more input (a name is text, so it needs no conversion) and print a receipt line such as Highway Dhaba: ₹1320 for 4 friends, using round(total) in the f-string.

Lesson quiz

Checked in your browser. Nothing is sent or saved.

5 questions

Question 1 · easy

In a Python program, what happens to a line that starts with #?

Question 2 · easy

Chai costs ₹12 a cup. The customer types 3 and presses Enter. What does print show? (The question text is not counted.)

cups = int(input("How many cups? "))
print(cups * 12)

Question 3 · medium

Which of these lines stops with a NameError?

Question 4 · medium

Two friends split ₹13. What does this program print?

share = 13 / 2
print(round(share))

Question 5 · hard

What does this program print?

print("Tea, coffee", "and", 2, "samosas")

Asking the user: input and conversionThat was the free module. Tell me when the course opens