← VCE Software Development U3O1

Range checking in code

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.

Understanding Range checking in code in VCE Software Development U3O1

What it does: Using <= at both ends means 1 and 100 are valid. Using < would reject them. Dates can be compared like numbers, so a range check works with a start and end date. A range check compares the value against a lower and an upper limit. 1 <= age <= 120 accepts every age from 1 to 120, including both limits. No number is both below 1 and above 120, so 'and' would never be true. 'or' correctly catches values outside either limit. Constants hold the limits in one place, so the range can be changed without hunting through the code.

Common mistakes: 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'. 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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 114–124) and word search (Modules 114–141).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which condition is a range check that accepts ages from 1 to 120 inclusive?
if 1 <= age <= 120:if age == 1 or 120:
Matches
if 1 < age < 120:
↓
Uses < instead of <=, so the boundary values 1 and 120 are wrongly rejected when the range should be inclusive.
Applying
Meanings
Term
1 <= age <= 120
Meaning
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.
Practice
MIN_AGE = 1
Testing
Gaps
if 1 <= age 120:
Check Answer
Spots
1age = int(input("Age: "))
2if 1 <= age >= 120:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Roadrunner' badge — Streak Runner, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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: ✓
A range check compares the value against a lower and an upper limit. 1 <= age <= 120 accepts every age from 1 to 120, including both limits.
2. A program uses the check 1 <= age <= 120. Which age is rejected as invalid?
A. 120
B. 121 ✓
Both limits are inclusive because of <=, so 1 and 120 pass. 121 is above the upper limit, so it is rejected.
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
Testing that a value is not empty is an existence check. A range check tests the lower and upper limits.
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:
Using < for the lower limit excludes 0. Use <= when the boundary value itself must be accepted (inclusive).
5. Which range check is better practice for a maximum quantity limit?
A. if MIN_QTY <= qty <= MAX_QTY: ✓
B. if 1 <= qty <= 50:
Using constants for the limits means that the values are named and set in one place. If the limits change, you only update the constant, not every check.

Match the Pairs

if 1 <= age <= 120: → Checks that age is between 1 and 120, including both limits
if MIN_AGE <= age <= MAX_AGE: → Range check that uses named constants for the lower and upper limits
Inclusive boundaries: the minimum and maximum values themselves are accepted → valid = 1 <= mark <= 100
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):
if 1 < age < 120: → Uses < instead of <=, so the boundary values 1 and 120 are wrongly rejected when the range should be inclusive.
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.

Key Term

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.

Fill in the Blanks

1. if 1 <= age <= 120: print("Valid age")
Hint: Type the comparison operator that keeps the upper limit inclusive
2. LOW = 1 HIGH = 120 if LOW <= age <= HIGH:
Hint: Type the constant that holds the upper limit of the range
3. entry = "2026-06-15" if "2026-01-01" <= entry <= "2026-12-31":
Hint: Type the variable holding the date being tested, placed between the two limits
4. if age < 1 or age > 120: print("Invalid age")
Hint: Type the logical keyword that rejects a value outside either limit
5. age = int(input("Age: ")) while not 1 <= age <= 120: age = int(input("Try again: "))
Hint: Type the keyword that reverses the range test so the loop repeats for out-of-range values

Spot the Error

Question 1
A age = int(input("Age: "))
B if 1 <= age >= 120:
C print("Valid")
D else:
E print("Invalid")
Line 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'.
Question 2
A MIN = 1
B MAX = 120
C if age < MIN and age > MAX:
D print("Out of range")
Line 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.
Question 3
A score = input("Score: ")
B if 0 <= score <= 100:
C print("Accepted")
Line A — input() returns a string, so comparing it with numbers causes a TypeError. Convert it first with int(): score = int(input("Score: ")).
Question 4
A temp = float(input("Temp: "))
B if 50 <= temp <= -10:
C print("OK")
D else:
E print("Rejected")
Line 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.
Question 5
A MAX_MARK == 50
B mark = int(input("Mark: "))
C if 0 <= mark <= MAX_MARK:
D print("Valid")
Line 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.

Student with a class code? Join your class →