hands-on labIDE in your browser

The Price Table: Nested Loops

A price table for Bean There's wall: what 1 to 5 of each item costs, in lined-up columns.

time
30 min
checked steps
5
level
Beginner
setup
None

$29.99a month with Pro

Pro opens all 5 steps of this lab and every other lab. Cancel any time.

no account needed · Pro opens all 5 steps

See Pro plans

The job

Offices order coffee by the tray, and the person sent to fetch it always asks the same thing: how much are five? Sam wants the answer on the wall, a table with every item and every amount.

5 steps, each checked when you finish it

A check runs your work at the end of every step. Hints and the full solution are there if you get stuck.

  1. 1

    One row

    Start with the flat whites: the name, then what 1, 2, 3, 4 and 5 of them cost, all on one line:

    read this step below ↓

  2. 2

    The header

    A row of prices needs the amounts above it.

  3. 3

    Every item

    The croissants and the brownies need rows too.

  4. 4

    The office deal

    To win the office orders, Sam's deal is every fifth item free: buy five, pay for four.

  5. 5

    Up to ten

    Bigger offices order ten at a time.

Step 1 as it appears in the lab

The lab’s own text. The hint and the solution stay inside the lab.

Step 1: One row

Start with the flat whites: the name, then what 1, 2, 3, 4 and 5 of them cost, all on one line:

Flat white    £3.20  £6.40  £9.60 £12.80 £16.00

print ends every line by going to a new one. print("Flat white", end="") ends with nothing instead, so the next print carries on the same line. Print the name that way, then the prices in a loop the same way, and after the loop a bare print() ends the line.

The columns line up the way the Receipt's did: the name takes 12 characters (:<12) and each price 7 (:>7). The prices are in pence, so the Receipt's f"£{total // 100}.{total % 100:02d}" turns them into pounds.

table.py, the file you edit6 lines
# The price table for the wall at Bean There: what 1 to 5 of each item costs.
FLAT_WHITE = 320   # prices in pence, as on the receipt
CROISSANT = 245
BROWNIE = 295

# TODO (Step 1): one line: "Flat white", then what 1 to 5 flat whites cost

what you get

  • An editor and terminal in your browser
  • A check at the end of every step
  • Hints and the full solution
  • Nothing to install or configure

before you start

  • Stock Countdown: for loops and range

Part of the Learn Python track

Lab 14 of 15. Learn Python by running a café till: receipts, orders, discounts and the Friday rush. Nothing to install.

Questions about this lab

What does end="" do in print?

print normally ends with a new line. With end="" it ends with nothing, so the next print carries on the same line.

Do I need to install anything?

No. Python runs right in your browser, in the lab's editor and terminal.

What you do in this lab

You print a table with Python: one row with print's end, a header with the column numbers, then a for loop inside a for loop, one row per item and one column per amount. Format codes line the columns up, and one variable decides how wide the table is.