Project: a tea-stall bill
🌍 In real life. Rahim runs a tea stall near a college. Tea is 15 taka a cup and a singara is 10 taka. At rush hour he adds up bills in his head, and sometimes he gets them wrong. He also wants to reward big orders: 5% off a bill of 100 taka or more, and 10% off a bill of 200 taka or more.
In this project you'll write Rahim's bill program. It uses everything from World 1: input(), numbers, variables, f-strings and if/elif/else.
The plan in one sentence: read the order, work out the costs, decide the discount, then print a neat bill.
Its words:
- constant: a value that never changes while the program runs, like a price. Python's habit is to name it in CAPITALS:
TEA = 15 - input → process → output: the shape of almost every program: read something, work on it, show the result
Step by step, the plan:
- Read: ask for the number of cups and singaras, and turn the answers into numbers.
- Work out: each cost is the count times the price; the total is their sum.
- Decide: pick the discount with an
if/elif/elsechain, strictest rule first. - Print: show each line of the bill with an f-string, money with 2 decimal places.
Here is step 3 for a 110 taka bill. Watch which rule wins:
Steps 1 and 2: read and work out. Convert each answer with int() straight away, so everything after it works with numbers. Keep each cost in its own variable: the bill prints them, and the total adds them.
TEA = 15
SINGARA = 10
cups = int(input("Cups of tea: "))
singaras = int(input("Singaras: "))
tea_cost = cups * TEA
singara_cost = singaras * SINGARA
total = tea_cost + singara_cost
print(f"Tea: {cups} x {TEA} = {tea_cost} taka")
print(f"Singara: {singaras} x {SINGARA} = {singara_cost} taka")
print(f"Total: {total} taka")Steps 3 and 4: decide and print. The chain checks total >= 200 before total >= 100: a 250 taka bill is also "100 or more", so the other order would always give the smaller discount. A percentage is total * 5 / 100, and / gives a float, so the money lines use :.2f.
TEA = 15
SINGARA = 10
cups = int(input("Cups of tea: "))
singaras = int(input("Singaras: "))
tea_cost = cups * TEA
singara_cost = singaras * SINGARA
total = tea_cost + singara_cost
if total >= 200:
discount = total * 10 / 100
elif total >= 100:
discount = total * 5 / 100
else:
discount = 0
print(f"Tea: {cups} x {TEA} = {tea_cost} taka")
print(f"Singara: {singaras} x {SINGARA} = {singara_cost} taka")
print(f"Total: {total} taka")
print(f"Discount: {discount:.2f} taka")
print(f"To pay: {total - discount:.2f} taka")Why build it this way.
- Prices at the top. When tea goes up to 20 taka, you change one line, and every calculation follows.
- Work out first, print last. The numbers are ready before any line of the bill is shown, so the bill can't print a half-finished total.
- One chain for one decision. Exactly one discount rule applies, which is exactly what
if/elif/elseguarantees.
Mistakes and edge cases.
- Forgetting int().
"2" * 15doesn't raise an error: it repeats the text,222222222222222. Convert the answers. - The rules in the wrong order. With
total >= 100first, nobody ever gets 10% off. - Percent as %.
total % 10is a remainder, not 10 percent. Writetotal * 10 / 100. - Strange orders. A negative number of cups, or an empty order, should get a message instead of a bill. That's your third task.
Quiz
What does this print?
total = 110 if total >= 200: print("10% off") elif total >= 100: print("5% off") else: print("no discount")A 250 taka bill should get 10% off, but this prints 5. Which line has the mistake?
total = 250 if total >= 100: rate = 5 elif total >= 200: rate = 10 print(rate)The user typed 2, but the program forgot int(). What does this print?
cups = "2" print(cups * 3)What does this print?
total = 110 discount = total * 5 / 100 print(f"To pay: {total - discount:.2f} taka")Why does the program keep the prices in TEA and SINGARA at the top?
- So a price change is made in one place
- Python needs numbers in capital letters
- It makes the program run faster
Coding tasks
Step 1: read the order. Read the number of cups and the number of singaras (two
input()calls, in that order), work out the costs, and print the first three lines of the bill. For 2 cups and 3 singaras:Tea: 2 x 15 = 30 taka Singara: 3 x 10 = 30 taka Total: 60 takaStep 2: add the discount. 200 taka or more gets 10% off, 100 taka or more gets 5% off, and anything less gets nothing. After the three lines, print the discount and the amount to pay, both with 2 decimal places. For 6 cups and 2 singaras the last two lines are:
Discount: 5.50 taka To pay: 104.50 takaStep 3: check the order. Before making a bill:
- if either number is negative, print only
Order can't be negative - if both are 0, print only
Nothing ordered - otherwise, print the full bill from Step 2
- if either number is negative, print only