Python Quest

Track 1 · Lesson 11 of 11

Mini project: a shop receipt

Combine everything from this track to build a complete receipt program, one tested milestone at a time.

About 50 minBeginner5 exercisesFree

You will be able to

  • Plan a larger program as a sequence of milestones
  • Combine input, conversion, variables, arithmetic and formatted output in one program
  • Extend a working program step by step, testing after each step
  • Produce neatly formatted money values with f-strings

Time to put the whole track together. You'll build the till program for Date Palm Grocery, a small neighbourhood shop. The cashier types the customer's name, two items with their quantities and prices, and the amount the customer pays. The program prints a complete receipt with VAT and change.

You won't write it all at once. Professional programmers build larger programs in milestones: small steps that each leave a working, tested program. If something breaks, the problem must be in the step you added last. Each milestone below starts from the solution to the previous one.

The finished receipt

For a customer named Lina who buys 2 bags of Rice 5kg at 32.50 SAR and 1 Dates box at 24 SAR, and pays 110 SAR, the finished program prints:

================================
DATE PALM GROCERY
Customer: Lina
================================
Rice 5kg x 2 @ 32.50 = 65.00
Dates box x 1 @ 24.00 = 24.00
--------------------------------
Subtotal: 89.00
VAT 15%: 13.35
Total: 102.35 SAR
Paid: 110.00 SAR
Change: 7.65 SAR
Thank you, Lina!

Both divider lines are 32 characters long.

The plan

Following Writing small programs, first decide what goes in, what comes out, and how to get from one to the other.

Inputs, in this order: customer name (text); item 1 name (text), quantity (whole number), unit price (decimal); the same three for item 2; amount paid (decimal).

Calculations:

  • line total = quantity × unit price, for each item
  • subtotal = line total 1 + line total 2
  • VAT = subtotal × 0.15
  • total = subtotal + VAT
  • change = amount paid − total

Output: the receipt above, with every money value shown with two decimals (:.2f).

Hand check: 2 × 32.50 = 65.00; 65.00 + 24.00 = 89.00; 15% of 89.00 is 13.35; total 102.35; change from 110 is 7.65. These are the numbers your program must produce.

The trickiest line

The item lines pack four values into one formatted line. Here is that line on its own, with fixed values, so you can experiment with it before you need it:

Building one receipt line with an f-string
item = "Rice 5kg"
qty = 2
price = 32.5
line_total = qty * price
print(f"{item} x {qty} @ {price:.2f} = {line_total:.2f}")

Try it: Change qty to 3 and price to 19.99. Check the line total by hand.

Notice that qty is shown without :.2f (it's a count, so 2, not 2.00), while both money values use :.2f.

Your program reads the values with input() instead. The checker types the inputs in the order listed in the plan, and only compares what your program prints, so you can word your prompts however you like.

Guided practice · ●difficulty 1 of 5 · about 4 min

Milestone 1: the header

Start the receipt program. Ask for the customer's name, then print the header. For the name Lina:

================================
DATE PALM GROCERY
Customer: Lina
================================

Each divider is 32 = characters; build it with "=" * 32.

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Milestone · ●●difficulty 2 of 5 · about 6 min

Milestone 2: the first item

Extend your program. After the header, read the first item's name, quantity (a whole number) and unit price (may have decimals), in that order. Then print the item line. For inputs Lina, Rice 5kg, 2, 32.5:

================================
DATE PALM GROCERY
Customer: Lina
================================
Rice 5kg x 2 @ 32.50 = 65.00

The line format is: name, x , quantity, @ , unit price with two decimals, = , line total with two decimals.

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Milestone · ●●difficulty 2 of 5 · about 7 min

Milestone 3: second item and subtotal

Add a second item, read and printed in exactly the same way as the first. Then print a divider of 32 - characters and the subtotal of both line totals. For inputs Lina, Rice 5kg, 2, 32.5, Dates box, 1, 24:

================================
DATE PALM GROCERY
Customer: Lina
================================
Rice 5kg x 2 @ 32.50 = 65.00
Dates box x 1 @ 24.00 = 24.00
--------------------------------
Subtotal: 89.00

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Challenge · ●●difficulty 2 of 5 · about 6 min

Milestone 4: VAT and total

Add 15% VAT. After the subtotal, print the VAT amount and the total including VAT:

VAT 15%: 13.35
Total: 102.35 SAR

(That's for the same inputs as milestone 3, where the subtotal is 89.00.) Calculate the VAT from the subtotal, and store both the VAT and the total in variables.

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Edge cases · ●●difficulty 2 of 5 · about 7 min

Milestone 5: payment and change

Finish the receipt. Read the amount the customer pays (it may have decimals), then print the amount paid, the change and a thank-you line using the customer's name:

Paid: 110.00 SAR
Change: 7.65 SAR
Thank you, Lina!

(That's for the example inputs followed by 110.) You can assume the customer always pays at least the total. Make sure an exact payment shows Change: 0.00 SAR.

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Summary

  • Larger programs are built in milestones, testing after each one.
  • A plan lists the inputs, the calculations and the exact output before any code is written.
  • Values stored in variables early (like the customer's name or the total) can be reused later in the program.
  • f-strings with :.2f give consistent money formatting; counts stay as whole numbers.
  • You've used every idea in this track: print(), arithmetic, strings, variables, input, conversion, planning and debugging.