…
…
Price a table's order from the menu, apply a tiered discount, print an aligned receipt and split the bill between people.
Corner Café's cash register still works out bills with pen and paper. In this project you start from the order for table 5 and build a small bill program step by step: the menu, counting the order, the subtotal, a tiered discount, an aligned receipt and splitting the bill.
Lessons used: numbers and round (3), conditions (6–8), loops and accumulating (10–12), formatting and alignment (22), lists (23–24), reading, looping and counting in a dictionary (26–27). No functions needed.
The cells build on each other: you use the menu dictionary you define in the first step in the later steps. Running a cell again does not change the result. The code you write is saved; when you reopen the project, the earlier cells run on their own.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
You will keep Corner Café's menu in a dictionary: the key is the item name and the value is its price in dollars. The menu has six items: tea $1.25, latte $4.25, lemonade $3.25, brownie $3.75, cookie $2.25 and water $0.75.
Build the menu dictionary, then print the number of items and the price of a latte.
This step's code cell is for members.Sign up free
The server wrote down the order for table 5 in a list, in the order the customers asked. The list also has an item that is not on the menu: a customer asked for hot chocolate, but the café does not serve it.
Loop over the list. If the item is not on the menu, print Not on the menu: hot chocolate and move on to the next item (continue). If it is on the menu, add one to that item's count in the counts dictionary. For a key that is not in the dictionary yet, counts.get(item, 0) returns 0.
This step's code cell is for members.Sign up free
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The amount for each item is price × quantity. counts.items() lets you loop over the dictionary as (item, quantity) pairs (lesson 27).
Start a subtotal variable at 0, add each item's amount to it, and print it.
This step's code cell is for members.Sign up free
The café gives a discount that grows as the bill grows:
| Subtotal | Discount |
|---|---|
| $25 or more | 15% |
| $15 or more | 10% |
| $7.50 or more | 5% |
| Under $7.50 | none |
Use if / elif / else to find the discount rate (rate). The order of the conditions matters: list them from largest to smallest, because the first true condition runs and the rest are skipped. Round the discount amount to cents (round(..., 2)) and calculate the total due.
This step's code cell is for members.Sign up free
On a receipt the columns must line up. In an f-string, the alignment goes after the colon: {item:<10} puts the text in a 10-character field aligned to the left, and {quantity:>3} puts the number in a 3-character field aligned to the right. {amount:>7.2f} puts a number in a 7-character field with two decimal places, right-aligned; ^ centers (lesson 22). The title lines are centered with {'CORNER CAFÉ':^32}.
Every item line and total line on the receipt is 32 characters wide. You write the item lines and the "Total due" line. The format of an item line is: {item:<10}{quantity:>3} x {price:>6.2f} = {amount:>7.2f}. The receipt shows plain numbers; all amounts are in dollars.
This step's code cell is for members.Sign up free
The three friends at the table want to split the bill equally and leave a tip of 10% of the total due.
Round the tip to cents, then calculate the total with tip and the amount per person. Round the per-person amount to cents too. Print the results with two decimal places.
This step's code cell is for members.Sign up free