Range checking in Python

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

Range checking in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which condition correctly checks that mark is between 0 and 100 inclusive?
A if 0 >= mark >= 100:
B if 0 <= mark <= 100:
2. Python allows a chained comparison such as 0 <= mark <= 100.
A True
B False
3. mark = 100. Is the range check 0 <= mark <= 100 True or False?
A True
B False
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:
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

Match the Pairs

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

Left columnRight column
1if 0 <= mark <= 100: AThe second comparison is missing the variable name. It should be mark <= 100, or use 0 <= mark <= 100.
2if age < 0 or age > 120: Belse: print("Mark must be 0 to 100")
3Boundary values 0 and 100 must be accepted, so use <= rather than < on both sides CDetects an out-of-range age: True when age is below the minimum or above the maximum
4Show an error message when the number is outside the allowed range Dif 0 <= mark <= 100:
5if 0 < mark < 100: EUses < instead of <=, so the boundary values 0 and 100 are wrongly rejected. It should be 0 <= mark <= 100.
6if mark >= 0 and <= 100: FA chained comparison range check that is True only when mark is from 0 to 100 inclusive

Definitions

if 0 <= mark <= 100:

Fill in the Blanks

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

1.
if _ <= mark <= 100:
2.
if 0 <= mark __ 100:
3.
__ 0 <= mark <= 100: print("Mark accepted")
4.
if ___ 0 <= mark <= 100: print("Mark must be 0 to 100")
5.
if mark >= 0 ___ mark <= 100: print("Valid")

Spot the Error

Shade in the letter of the line that contains an 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")
Question 2
A if 0 <= score < 100:
B print("Accepted")
C else:
D print("Out of range")
Question 3
A age = int(input("Age: "))
B if age < 5 or age > 18:
C 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")
Question 5
A low = 1
B high = "10"
C num = int(input("Number: "))
D if low <= num <= high:
E print("In range")

Answer Key

Range checking in Python
Section A: Quick Questions
Q1
B — if 0 <= mark <= 100:
Q2
A — True
Q3
A — True
Q4
A — if 0 < mark <= 100:
Q5
B — False, so show an error message
Section B: Match the Pairs
Q1
if 0 <= mark <= 100: → A chained comparison range check that is True only when mark is from 0 to 100 inclusive
Q2
if age < 0 or age > 120: → Detects an out-of-range age: True when age is below the minimum or above the maximum
Q3
Boundary values 0 and 100 must be accepted, so use <= rather than < on both sides → if 0 <= mark <= 100:
Q4
Show an error message when the number is outside the allowed range → else: print("Mark must be 0 to 100")
Q5
if 0 < mark < 100: → Uses < instead of <=, so the boundary values 0 and 100 are wrongly rejected. It should be 0 <= mark <= 100.
Q6
if mark >= 0 and <= 100: → The second comparison is missing the variable name. It should be mark <= 100, or use 0 <= mark <= 100.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
if 0 <= mark <= 100:
Q2
if 0 <= mark <= 100:
Q3
if 0 <= mark <= 100: print("Mark accepted")
Q4
if not 0 <= mark <= 100: print("Mark must be 0 to 100")
Q5
if mark >= 0 and mark <= 100: print("Valid")
Section E: Spot the Error
Q1
B — The if statement is missing its colon at the end. It should be: if 0 <= mark <= 100:
Q2
A — Using < 100 rejects the boundary value 100, which is a valid score. The upper check should be <= 100 so that 100 is accepted.
Q3
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").
Q4
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:
Q5
B — The maximum is stored as a string, so comparing num with it causes a TypeError. The boundary must be a number: high = 10