hands-on labIDE in your browser

Drink Sizes: match and case

Price Bean There's flat whites by size and milk with match: a case per size, short answers with |, a case _ for anything else, answers typed any way, and a second match for the milk.

time
25 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

Bean There now sells three sizes, and oat or soy milk costs extra. Customers say "large", "L", " Medium " and sometimes "huge". The till has to price every one it understands and politely turn away the rest.

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

    match a size

    A small flat white is £3.20, a medium costs 40p more and a large 80p more.

    read this step below ↓

  2. 2

    Short answers with |

    Regulars don't say "large", they say "L".

  3. 3

    case _: anything else

    A customer asked for a "huge" flat white.

  4. 4

    Typed any way

    The size comes from the customer now: sizes.py asks for it.

  5. 5

    Milk

    After the size the till asks about milk.

Step 1 as it appears in the lab

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

Step 1: match a size

A small flat white is £3.20, a medium costs 40p more and a large 80p more. With if and elif that's three tests of size == ..., one after another. When every test compares the same variable with a different value, Python has a neater tool, match:

match size:
    case "small":
        extra = 0
    case "medium":
        extra = 40
    case "large":
        extra = 80

Python compares size with each case from the top and runs the first one that matches. In sizes.py, work out the extra, then print the price with the size's name:

Large flat white: £4.00

.title() gives size its capital: size.title() is "Large". The check tries all three sizes.

sizes.py, the file you edit7 lines
# Bean There's flat whites by size. Prices and extras in whole pennies.
FLAT_WHITE = 320

size = "large"

# TODO (Step 1): with match, set the extra for the size (small 0, medium 40, large 80) and print:
#   Large flat white: £4.00

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

  • Opening Hours: if, elif, else and truthiness

Part of the Learn Python track

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

Questions about this lab

Which Python version has match?

match and case arrived in Python 3.10. This lab runs Python 3.11 right in your browser.

Do I need to install anything?

No. Python runs right in your browser.

What you do in this lab

You use Python's match statement where an elif chain would repeat itself: one case per size, several spellings in one case with |, a case _ that catches everything else, answers cleaned with strip() and lower() before matching, and a second match for the milk.