hands-on labIDE in your browser

Happy Hour: True, False and if

Bean There's flat whites cost less from 3pm to 5pm. Teach the till to tell: comparisons that give True or False, if and else, elif for the time of day, or for members, and a closed sign that comes first.

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

Sam wants a happy hour: flat whites at £2.50 instead of £3.20, from 3pm until 5pm. Members get the happy price all day, the menu changes with the time of day, and outside opening hours the till should say so. Every one of those is a decision, and the till makes them with if.

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

    True or False

    Is it happy hour?

    read this step below ↓

  2. 2

    if and else

    Now the till acts on the answer.

  3. 3

    elif: three times of day

    The board above the counter changes through the day: the breakfast menu until 11:00, the lunch menu until 15:00, then the afternoon menu.

  4. 4

    Members: or

    Sam's loyalty members pay the happy price all day.

  5. 5

    Closed comes first

    At 6:00 this morning the cleaner pressed a key on the till and it offered the breakfast menu.

Step 1 as it appears in the lab

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

Step 1: True or False

Is it happy hour? A till can't answer "sort of": it needs yes or no. In Python those are True and False, and a comparison gives you one of them:

print(16 > 15)      # True
print(16 == 15)     # False: == asks "are they equal?"
print(16 != 15)     # True: != asks "are they different?"

= puts a value in a variable; == compares. Mixing them up is the most common slip in this level, and Python points it out with a SyntaxError if you write if hour = 15.

Happy hour runs from 15:00 up to, but not including, 17:00. Python lets you chain two comparisons the way maths does:

15 <= hour < 17

In prices.py, keep that in a variable called happy and print it:

Happy hour: True

Change hour to 14 and run it, then 16, then 17, to watch it flip. The check tries those hours too.

prices.py, the file you edit9 lines
# Bean There's prices, in whole pennies. Sam runs a happy hour from 3pm until 5pm.
FLAT_WHITE = 320
HAPPY_PRICE = 250

hour = 15   # the hour on the till's clock, 0 to 23

# TODO (Step 1): keep whether it's happy hour (15:00 up to, but not including, 17:00) in a
# variable called happy, and print it:
#   Happy hour: True

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

  • Level 1 of the Python course: variables, f-strings, whole-penny money

Part of the Learn Python track

Lab 6 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 elif mean in Python?

It's short for else if: Python tests it only when every if and elif above it was False, so exactly one branch runs.

Do I need to install anything?

No. Python runs right in your browser.

What you do in this lab

You make a script decide. Comparisons give True or False, chained comparisons read like maths, if and else pick a price, elif picks one of several menus, or lets members in, and the order of the checks decides which one wins. Every check tries the till at the hours where a decision flips.