Combining validation checks in a reusable function

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

Combining validation checks in a reusable function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Why put existence, type and range checks inside one function like get_valid_age()?
A So the same checks can be reused every time an age is needed
B So the program only has to check the first age entered
2. Which order should get_valid_age() apply its checks?
A Existence, then type, then range
B Range, then type, then existence
3. Which line is the best way to use a function that returns a checked age?
A age = get_valid_age()
B get_valid_age = age()
4. A function checks an age and returns True or False. What does returning False tell the program?
A The data passed every validation check
B The data failed at least one validation check
5. Which range check belongs inside get_valid_age() for ages 5 to 100?
A if age >= 5 or age <= 100:
B if age >= 5 and age <= 100:

Match the Pairs

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

Left columnRight column
1def get_valid_age(): AThe function prints the age instead of returning it, so the caller gets no checked value back. Use return age.
2return age Bif text == "": return False
3Run the checks in order: existence first, then type, then range CThe type check (int) runs before the existence check, so empty input crashes with an error. Check that the input exists first.
4Reuse the same function for each input DSends the checked age back to the code that called the function once it has passed every check
5age = int(text) if text == "": return False EDefines a reusable function that bundles all the age checks together so they can be called for every input
6def get_valid_age(): age = int(input("Age: ")) if 0 <= age <= 120: print(age) Fage2 = get_valid_age()

Definitions

def get_valid_age():

Fill in the Blanks

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

1.
age = input("Age: ") if age __ "": return False
2.
age = input("Age: ") if not age._______(): return False
3.
age = int(age) if age < 0 or age _ 120: return False
4.
if 0 <= age <= 120: ______ age return False
5.
first = _____________() second = get_valid_age() print(first + second)

Spot the Error

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

Question 1
A def get_valid_age():
B text = input("Age: ")
C if text = "":
D return False
E return True
Question 2
A def get_valid_age()
B age = input("Age: ")
C if not age.isdigit():
D return False
E return True
Question 3
A def get_valid_age():
B age = int(input("Age: "))
C if age < 0 and age > 120:
D return False
E return age
Question 4
A def get_valid_age():
B text = input("Age: ")
C if not text.isdigit():
D return True
E age = int(text)
F return 0 <= age <= 120
Question 5
A def get_valid_age():
B text = int(input("Age: "))
C if text == "":
D return None
E return int(text)

Answer Key

Combining validation checks in a reusable function
Section A: Quick Questions
Q1
A — So the same checks can be reused every time an age is needed
Q2
A — Existence, then type, then range
Q3
A — age = get_valid_age()
Q4
B — The data failed at least one validation check
Q5
B — if age >= 5 and age <= 100:
Section B: Match the Pairs
Q1
def get_valid_age(): → Defines a reusable function that bundles all the age checks together so they can be called for every input
Q2
return age → Sends the checked age back to the code that called the function once it has passed every check
Q3
Run the checks in order: existence first, then type, then range → if text == "": return False
Q4
Reuse the same function for each input → age2 = get_valid_age()
Q5
age = int(text) if text == "": return False → The type check (int) runs before the existence check, so empty input crashes with an error. Check that the input exists first.
Q6
def get_valid_age(): age = int(input("Age: ")) if 0 <= age <= 120: print(age) → The function prints the age instead of returning it, so the caller gets no checked value back. Use return age.
Section C: Definitions
Q1
def get_valid_age(): — A function that runs existence, type and range checks in order, then returns a valid age (or True/False) so you can reuse the same checks for every input.
Section D: Fill in the Blanks
Q1
age = input("Age: ") if age == "": return False
Q2
age = input("Age: ") if not age.isdigit(): return False
Q3
age = int(age) if age < 0 or age > 120: return False
Q4
if 0 <= age <= 120: return age return False
Q5
first = get_valid_age() second = get_valid_age() print(first + second)
Section E: Spot the Error
Q1
C — The existence check compares text with an empty string, so it needs the comparison operator == instead of the assignment operator =. It should be: if text == "":
Q2
A — A function definition line must end with a colon. It should be: def get_valid_age():
Q3
C — No number is both below 0 and above 120, so 'and' means the range check never catches an invalid age. It needs 'or': if age < 0 or age > 120:
Q4
D — The type check should reject text that is not made of digits, so it must return False. Returning True would tell the program that bad input is valid. It should be: return False
Q5
B — Converting to int straight away crashes on empty or non-numeric input before the existence and type checks can run. Keep the input as text first: text = input("Age: ")