…
…
Unit 2 task: turn Ada's rule sheet into a program; build the menu by hour, the tiered discount, and the member and coupon rules in the right order, test with nine cases, and apply the same idea to a movie ticket.
In unit 2 the program learned to make decisions: comparisons, and/or/not, if/elif/else, truth values and None. Now you will combine them in the real rules of Corner Café.
Ada has written the rules at the register on a sheet of paper:
| # | Rule |
|---|---|
| 1 | An order with an amount of 0 or less is invalid |
| 2 | The café is open from 8 to 22 (8 included, 22 excluded) |
| 3 | Before 12 the Breakfast menu, from 12 to 17 the Daytime menu, from 17 on the Evening menu |
| 4 | $25 and up gets 15 percent off; $15 and up gets 10; $7.50 and up gets 5 |
| 5 | Members or students get 5 extra points of discount when the amount is $5 or more |
| 6 | A customer with a coupon gets $1 more off; the total cannot go below 0 |
At the end of the task, for a $16 member order without a coupon that arrives at 10, your program will print:
OutputBreakfast | Discount 15% | Total: $13.60Lessons used: 6 (comparisons, and/or/not, one-branch if), 7 (if/elif/else, tier order, testing with boundary values), 8 (parentheses, truth values, None), 2–4 (variables, percentage calculation, f-strings).
How to work: First build the rules one by one (cells 1–4), then combine them (cell 5), and finally test with the table. Each cell starts with its own inputs; Check also tries your code with other values of these inputs, especially at the boundaries (hour 22, amount $7.50 and so on). Working in one example is not enough.
What you'll do
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Rules 2 and 3: the café is open from 8 to 22 (8 included, 22 excluded); before 12 it serves the Breakfast menu, from 12 to 17 the Daytime menu, and from 17 on the Evening menu.
Assign one of four values to the menu variable: "Closed", "Breakfast", "Daytime" or "Evening". Examples: hour 10 → Menu: Breakfast · hour 22 → Menu: Closed.
Check runs your code with hours 7, 8, 11, 12, 16, 17, 21 and 22, one at a time. Change the hour = 10 line and try other values yourself.
This step's code cell is for members.Sign up free
Rule 4: $25 and up gets 15 percent; $15 and up gets 10; $7.50 and up gets 5; below that there is no discount. Assign the discount to the rate variable as a whole-number percentage (0, 5, 10 or 15). Examples: 16 → Rate: 10
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Rate: 0The cell starts with its own amount line, so Check can try it with 7.25, 7.5, 14.75, 15, 24.75 and 25.
This step's code cell is for members.Sign up free
Rule 5: a member or a student gets an extra 5 points of discount when the amount is $5 or more. If someone is both a member and a student, the extra points are given once.
The skeleton takes the rate from cell 2 as base_rate and starts rate from it. Add 5 to rate when needed. Examples: ($16, member) → Rate: 15 · ($4.75, member) → Rate: 0.
This step's code cell is for members.Sign up free
Rule 6: first apply the discount by rate; if the customer has a coupon, take $1 more off; the total must not go below 0. Assign the result to the total variable.
If the coupon box is left empty, coupon is an empty string (""); if the box was never opened, it is None. Both mean "no coupon". Examples: ($16, rate 15, no coupon) → Total: $13.60 · ($0.75, rate 0, coupon "CAFE1") → Total: $0.00.
This step's code cell is for members.Sign up free
Combine the decisions from cells 1–4 into one program in this cell and add rule 1 at the very beginning. The output is one line:
Invalid orderWe are closedMenu | Discount N% | Total: $XAbove the cell there is an empty case table. Each row tests the limit of one rule. Before you write the code, write the output you expect for each row into the table; then change the inputs, run, and compare. Check tries the same nine cases (and one more) itself.
This step's code cell is for members.Sign up free
A new rule sheet, this time from a cinema:
The inputs are ready. Work out price; the print line is ready (Ticket: $8). Check also tries your code with other ages, days and student values (for example a 10-year-old child on Wednesday).
Think (no points): Which idea from the café program helped here too?
This step's code cell is for members.Sign up free