← VCE Data Analytics U4O1

Range checking in Python

A range check tests that a number lies between a minimum and maximum. This chained comparison is True only when mark is from 0 to 100 inclusive, so the boundary values 0 and 100 are accepted.

Understanding Range checking in Python in VCE Data Analytics U4O1

What it does: Using <= includes the boundary values, so a mark of exactly 0 or exactly 100 is valid. Using < would wrongly reject them. When the range check fails, the else branch runs and tells the user why the input was rejected and what range is allowed. A range check puts the minimum on the left and the maximum on the right: 0 <= mark <= 100. Reversing the operators means no number can pass. Python lets you chain comparisons, so one condition tests both the minimum and the maximum. Many other languages need two separate tests joined with 'and'. 101 is above the maximum of 100, so the condition is False. The else branch should print an error message such as 'Mark must be between 0 and 100'.

Common mistakes: The if statement is missing its colon at the end. It should be: if 0 <= mark <= 100:. Using < 100 rejects the boundary value 100, which is a valid score. The upper check should be <= 100 so that 100 is accepted.

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 63–83) and word search (Modules 63–105).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which condition correctly checks that mark is between 0 and 100 inclusive?
if 0 <= mark <= 100:if 0 >= mark >= 100:
Matches
if 0 < mark < 100:
↓
Uses < instead of <=, so the boundary values 0 and 100 are wrongly rejected. It should be 0 <= mark <= 100.
Applying
Meanings
Term
if 0 <= mark <= 100:
Meaning
A range check tests that a number lies between a minimum and maximum. This chained comparison is True only when mark is from 0 to 100 inclusive, so the boundary values 0 and 100 are accepted.
Practice
mark = 100
Testing
Gaps
if <= mark <= 100:
Check Answer
Spots
1mark = int(input("Mark: "))
2if 0 <= mark <= 100

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

The 'Horned Lizard' badge — Bug Squasher, 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 correctly checks that mark is between 0 and 100 inclusive?
A. if 0 >= mark >= 100:
B. if 0 <= mark <= 100: ✓
A range check puts the minimum on the left and the maximum on the right: 0 <= mark <= 100. Reversing the operators means no number can pass.
2. Python allows a chained comparison such as 0 <= mark <= 100.
A. True ✓
B. False
Python lets you chain comparisons, so one condition tests both the minimum and the maximum. Many other languages need two separate tests joined with 'and'.
3. mark = 100. Is the range check 0 <= mark <= 100 True or False?
A. True ✓
B. False
The operator <= includes the boundary value, so 100 is accepted. The boundary values 0 and 100 both pass.
4. Which range check wrongly REJECTS the boundary value 0 when the valid range is 0 to 100?
A. if 0 < mark <= 100: ✓
B. if 0 <= mark <= 100:
Using < instead of <= for the minimum excludes 0, so a valid mark of 0 would be rejected. Boundary values should be tested carefully.
5. mark = 101. What does the range check 0 <= mark <= 100 give?
A. True, so accept the mark
B. False, so show an error message ✓
101 is above the maximum of 100, so the condition is False. The else branch should print an error message such as 'Mark must be between 0 and 100'.

Match the Pairs

if 0 <= mark <= 100: → A chained comparison range check that is True only when mark is from 0 to 100 inclusive
if age < 0 or age > 120: → Detects an out-of-range age: True when age is below the minimum or above the maximum
Boundary values 0 and 100 must be accepted, so use <= rather than < on both sides → if 0 <= mark <= 100:
Show an error message when the number is outside the allowed range → else: print("Mark must be 0 to 100")
if 0 < mark < 100: → Uses < instead of <=, so the boundary values 0 and 100 are wrongly rejected. It should be 0 <= mark <= 100.
if mark >= 0 and <= 100: → The second comparison is missing the variable name. It should be mark <= 100, or use 0 <= mark <= 100.

Key Term

if 0 <= mark <= 100:
A range check tests that a number lies between a minimum and maximum. This chained comparison is True only when mark is from 0 to 100 inclusive, so the boundary values 0 and 100 are accepted.

Fill in the Blanks

1. if 0 <= mark <= 100:
Hint: Type the lowest value the range check allows for a mark.
2. if 0 <= mark <= 100:
Hint: Type the comparison operator that still accepts the maximum boundary value.
3. if 0 <= mark <= 100: print("Mark accepted")
Hint: Type the keyword that starts a decision.
4. if not 0 <= mark <= 100: print("Mark must be 0 to 100")
Hint: Type the keyword that reverses the range check so the error message runs for out-of-range marks.
5. if mark >= 0 and mark <= 100: print("Valid")
Hint: Type the logical operator that joins the two comparisons so both must be True.

Spot the Error

Question 1
A mark = int(input("Mark: "))
B if 0 <= mark <= 100
C print("Valid mark")
D else:
E print("Use 0 to 100")
Line B — The if statement is missing its colon at the end. It should be: if 0 <= mark <= 100:
Question 2
A if 0 <= score < 100:
B print("Accepted")
C else:
D print("Out of range")
Line A — Using < 100 rejects the boundary value 100, which is a valid score. The upper check should be <= 100 so that 100 is accepted.
Question 3
A age = int(input("Age: "))
B if age < 5 or age > 18:
C print("Age must be 5-18)
Line C — The error message string is missing its closing quote, so Python reports a syntax error. It should be print("Age must be 5-18").
Question 4
A temp = float(input("Temp: "))
B if -10 <= temp >= 50:
C print("Temp accepted")
D else:
E print("Out of range")
Line B — The second comparison points the wrong way. temp >= 50 only accepts high values. A range check needs the maximum with <=: if -10 <= temp <= 50:
Question 5
A low = 1
B high = "10"
C num = int(input("Number: "))
D if low <= num <= high:
E print("In range")
Line B — The maximum is stored as a string, so comparing num with it causes a TypeError. The boundary must be a number: high = 10

Student with a class code? Join your class →