Menu

Relational Operators

Boundary comparisons

A boundary is the value where a rule changes, such as the smallest number of tokens needed for a reward. The operators > and < exclude that boundary value, while >= and <= include it.

Including or excluding the boundary

Choose the operator by checking whether the boundary value should be accepted or rejected. If a club gives a free kit at 12 tokens or more, 12 must be accepted, so use tokens >= 12. Writing tokens > 12 would turn away somebody with exactly 12 tokens, which breaks the rule. If the rule were “more than 12 tokens”, then tokens > 12 would be correct and 12 would be rejected.

Rule in wordsComparisonBoundary 12
fewer than 12tokens < 1212 rejected
12 or fewertokens <= 1212 accepted
more than 12tokens > 1212 rejected
12 or moretokens >= 1212 accepted

Checking each side

Before choosing, test values either side of the boundary. For a rule accepting 12 or more, 11 should fail and 12 should pass. Try a value just above too: 13 should pass. Comparing tokens > 12 with tokens >= 12, only the second passes 12 and 13 while still failing 11.

When the values are whole numbers, a one-step adjustment can give exactly the same result. “More than 12” can be written tokens > 12 or tokens >= 13, because both accept the same whole numbers. In the same way, “12 or more” can be written tokens >= 12 or tokens > 11.

This program uses the 12-token boundary to decide whether a free kit is earned.

tokens = int(input("How many tokens? "))
if tokens >= 12:
    print("Free kit")
else:
    print("Not yet")

Output when the user types 12:

How many tokens? 12
Free kit

Practise this