Range checking in code

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Range checking in code

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which condition is a range check that accepts ages from 1 to 120 inclusive?
A if age == 1 or 120:
B if 1 <= age <= 120:
2. A program uses the check 1 <= age <= 120. Which age is rejected as invalid?
A 120
B 121
3. What does a range check test?
A That a number lies between a minimum and a maximum
B That a value is not left empty
4. You want a mark of 0 to be valid for a test scored out of 100. Which check is correct?
A if 0 <= mark <= 100:
B if 0 < mark <= 100:
5. Which range check is better practice for a maximum quantity limit?
A if MIN_QTY <= qty <= MAX_QTY:
B if 1 <= qty <= 50:

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1if 1 <= age <= 120: AUses < instead of <=, so the boundary values 1 and 120 are wrongly rejected when the range should be inclusive.
2if MIN_AGE <= age <= MAX_AGE: BChecks that age is between 1 and 120, including both limits
3Inclusive boundaries: the minimum and maximum values themselves are accepted Cvalid = 1 <= mark <= 100
4A range check on a date: the date must fall between a start date and an end date DRange check that uses named constants for the lower and upper limits
5if 1 < age < 120: Eif date(2025, 1, 1) <= entry <= date(2025, 12, 31):
6if age >= 1 or age <= 120: FUses or instead of and, so almost every number passes, including 0 and 121. Both limits must be satisfied together.

Definitions

1 <= age <= 120

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
if 1 <= age __ 120: print("Valid age")
2.
LOW = 1 HIGH = 120 if LOW <= age <= ____:
3.
entry = "2026-06-15" if "2026-01-01" <= _____ <= "2026-12-31":
4.
if age < 1 __ age > 120: print("Invalid age")
5.
age = int(input("Age: ")) while ___ 1 <= age <= 120: age = int(input("Try again: "))

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A age = int(input("Age: "))
B if 1 <= age >= 120:
C print("Valid")
D else:
E print("Invalid")
Question 2
A MIN = 1
B MAX = 120
C if age < MIN and age > MAX:
D print("Out of range")
Question 3
A score = input("Score: ")
B if 0 <= score <= 100:
C print("Accepted")
Question 4
A temp = float(input("Temp: "))
B if 50 <= temp <= -10:
C print("OK")
D else:
E print("Rejected")
Question 5
A MAX_MARK == 50
B mark = int(input("Mark: "))
C if 0 <= mark <= MAX_MARK:
D print("Valid")

Answer Key

Range checking in code
Section A: Quick Questions
Q1
B — if 1 <= age <= 120:
Q2
B — 121
Q3
A — That a number lies between a minimum and a maximum
Q4
A — if 0 <= mark <= 100:
Q5
A — if MIN_QTY <= qty <= MAX_QTY:
Section B: Match the Pairs
Q1
if 1 <= age <= 120: → Checks that age is between 1 and 120, including both limits
Q2
if MIN_AGE <= age <= MAX_AGE: → Range check that uses named constants for the lower and upper limits
Q3
Inclusive boundaries: the minimum and maximum values themselves are accepted → valid = 1 <= mark <= 100
Q4
A range check on a date: the date must fall between a start date and an end date → if date(2025, 1, 1) <= entry <= date(2025, 12, 31):
Q5
if 1 < age < 120: → Uses < instead of <=, so the boundary values 1 and 120 are wrongly rejected when the range should be inclusive.
Q6
if age >= 1 or age <= 120: → Uses or instead of and, so almost every number passes, including 0 and 121. Both limits must be satisfied together.
Section C: Definitions
Q1
1 <= age <= 120 — A range check: tests that a number lies between a minimum and a maximum (inclusive here). Values outside the limits, such as 0 or 121, are rejected as invalid.
Section D: Fill in the Blanks
Q1
if 1 <= age <= 120: print("Valid age")
Q2
LOW = 1 HIGH = 120 if LOW <= age <= HIGH:
Q3
entry = "2026-06-15" if "2026-01-01" <= entry <= "2026-12-31":
Q4
if age < 1 or age > 120: print("Invalid age")
Q5
age = int(input("Age: ")) while not 1 <= age <= 120: age = int(input("Try again: "))
Section E: Spot the Error
Q1
B — The second operator points the wrong way. 'age >= 120' only accepts ages of 120 or more, so most valid ages fail. The upper limit must be 'age <= 120'.
Q2
C — No number is both below the minimum and above the maximum, so 'and' never triggers and invalid values slip through. Use 'or' to reject a value outside either limit.
Q3
A — input() returns a string, so comparing it with numbers causes a TypeError. Convert it first with int(): score = int(input("Score: ")).
Q4
B — The limits are swapped. The minimum must come first and the maximum last, so it should be 'if -10 <= temp <= 50:'. As written, no value can pass.
Q5
A — A single = assigns a value to the constant. '==' only compares, so MAX_MARK is never created and the range check fails with a NameError. It should be MAX_MARK = 50.