…
…
Unit 1 task: write an aligned receipt with tax from a three-item order, split the bill among people to the cent, repair the line the intern broke, and run the same program with a stationery store's data.
Unit 1 is done: print, variables, calculations with numbers, type conversion, f-strings and error messages are now in your hands. Now you will combine them in one program for the first time.
At Corner Café, a table of three friends asks for the bill: 2 lattes, 1 cookie and 2 brownies. Ada at the register is tired of writing receipts by hand and asks you for a program that writes this receipt:
OutputCORNER CAFÉ
----------------------
Latte 2 8.50
Cookie 1 2.00
Brownie 2 3.50
----------------------
Subtotal 14.00
Tax (10%) 1.40
TOTAL 15.40Ada has a few more requests: the bill will be split among three people and nobody should be charged an extra cent; the tip line the intern tried to add to the receipt must be repaired; and the same program should be able to write the receipt of the stationery store next door.
Lessons used: 1 (print, line order), 2 (variables, assignment), 3 (*, /, //, %, round), 4 (int(), f-strings, :.2f, :<10, :>9.2f), 5 (reading an error message).
How to work: This is a unit task; it teaches no new topic and gives no worked example. The six cells build on each other: the quantities and amounts you find in the first cell are used by the later cells. In each cell the comments tell you what to do, and you write the code where it says ___. Run the cell, compare your output with the "Show expected output" area, then Check. If you get stuck, the hints point you to the relevant lesson. You can move on to lesson 6 without finishing the task; but trying to combine things is a different skill from knowing the lessons one by one.
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.
The prices are ready. The quantities come from the screen at the register, so they are text: "2" is not a number, it is a two-character string. Convert the three quantities to integers, then work out each item's line amount (price × quantity). The three print lines at the end of the cell print the amounts.
Warning: if a quantity stays text, 4.25 * "2" raises an error, but "2" * 4 does not; it repeats the string "2" four times side by side.
This step's code cell is for members.Sign up free
The line amounts are ready. Now work out three values: subtotal (the sum of the three line amounts), tax (10 percent of the subtotal) and total (subtotal + tax).
The tax rate is ready in the skeleton as TAX_RATE. Writing the name in capital letters is a habit in Python: it says "this value will not change during the program". Do not type the results by hand; work them out from the variables of the previous cell.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
This step's code cell is for members.Sign up free
Print the receipt Ada wants. The header is ready; you write the rest:
Subtotal, Tax (10%), TOTAL) left-aligned in 13 characters, amount right-aligned in 9 characters with two decimal placesThe calculation is done with numbers; the format is added only when printing: :.2f does not change the value of the variable. Do not type the amounts by hand; use the variables of the previous cells.
This step's code cell is for members.Sign up free
Three friends sit at the table and want to split the bill equally. If you divide 15.40 by 3 with /, you get 5.133333...; but you cannot pay a fraction of a cent. So do the calculation in whole cents with integers: convert the total to cents, then find the whole cents each person pays and the cents left over that cannot be shared. The print lines are ready.
When you multiply, 15.4 * 100 gives a float (1540.0000000000002); round turns it into an integer.
This step's code cell is for members.Sign up free
The intern wanted to add a tip line under the receipt: 10 percent of the total. The program does not run, and when it does, the amount is wrong. The expected line is:
Tip (10%): $1.54
There are three problems in two lines: two give an error message, and one gives a wrong result without any message. Each time, read the last line of the message, fix it and run again. When the program runs without errors, compare the result with the expected line. After you fix the two lines, delete the comment and the ___ line at the bottom.
This step's code cell is for members.Sign up free
The shop next door, a stationery store, wants the same receipt: 3 notebooks ($2.25), 4 pens ($0.50), 1 eraser ($0.75). The quantities again come from the screen as text. The header is DAISY STATIONERY and the alignment is the same as on the Corner Café receipt. TAX_RATE is ready from cell 2.
Build the code you wrote in cells 1–3 here with the new data: convert the quantities, work out the amounts and totals, and print the receipt.
Think (no points): How many lines did you have to change? Why so few?
This step's code cell is for members.Sign up free