hands-on labIDE in your browser

Loyalty Stamps: Decisions with Numbers

Bean There's stamp card: one stamp per coffee and every tenth coffee free.

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 gives a stamp for every coffee, and ten stamps earn a free one. Sam wants the till to keep the cards: say when a free coffee is due, say how many stamps a customer still needs, and not fall over when someone types the number of cups as a word.

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

    A free coffee?

    Priya's card has 7 stamps and she's buying 4 coffees.

    read this step below ↓

  2. 2

    else: how many to go

    Customers who don't get a free coffee ask the question anyway: "how many more?" Give the if an else that answers it:

  3. 3

    One stamp or two

    Jo has 5 stamps and buys 4 coffees.

  4. 4

    Two free coffees at once

    An office order: 14 coffees on a card with 7 stamps.

  5. 5

    Answers that aren't numbers

    From now on the barista types the number of cups.

Step 1 as it appears in the lab

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

Step 1: A free coffee?

Priya's card has 7 stamps and she's buying 4 coffees. That makes 11 stamps: a free coffee, and 1 stamp left on the card for next time.

Add the cups to the card first. stamps = stamps + cups works, and Python has a shorter way to write it:

stamps += cups      # the same as: stamps = stamps + cups

Then decide. When the card reaches STAMPS_FOR_FREE, the till announces the free coffee and takes ten stamps off the card. An if with no else is fine: when the test is False, Python skips the block and carries on.

Free coffee on the house!
Stamps on the card: 1

For someone with 2 stamps buying 3 cups, only the second line prints: Stamps on the card: 5. The check tries both, and a card that lands on exactly 10.

stamps.py, the file you edit10 lines
# Bean There's loyalty card: one stamp per coffee, and every 10 stamps earn a free coffee.
STAMPS_FOR_FREE = 10

stamps = 7    # stamps already on Priya's card
cups = 4      # coffees she's buying now

# TODO (Step 1): add the cups to the card. At STAMPS_FOR_FREE or more, print the free coffee line
# and take STAMPS_FOR_FREE off the card. Then always print what's on the card:
#   Free coffee on the house!
#   Stamps on the card: 1

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

  • Happy Hour: if, elif and else

Part of the Learn Python track

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

Questions about this lab

How do I check that input is a number in Python?

For whole numbers, answer.isdigit() is True only when every character is a digit. Check it before calling int(), and ask again or say what to type when it's False.

Do I need to install anything?

No. Python runs right in your browser.

What you do in this lab

You write decisions that depend on numbers: an if that fires at ten stamps, an else that counts what's left, a one-line if for singular and plural, floor division and the remainder for several free coffees at once, and isdigit() to check an answer before int() can trip on it.