Combining validation checks in a validation function

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

Combining validation checks in a validation function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A validation function is checking a typed age. Which check order is correct?
A Existence, then type, then range
B Range, then type, then existence
2. What does is_valid_age(text) return when every check passes?
A True
B The age as text
3. Which line is the existence check in is_valid_age(text)?
A if int(text) > 120: return False
B if text == "": return False
4. Why should the type check come before the range check?
A Range checks run faster when they come second
B Comparing text such as "abc" with a number would cause a runtime error
5. Which approach lets you reuse validation for many inputs?
A Put the checks in one function and call it for each input
B Copy and paste the checks next to every input() call

Match the Pairs

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

Left columnRight column
1def is_valid_age(text): ADefines a validation function that takes the user's text and will return True or False
2if text == "": return False BThe existence check only prints a message and does not return False, so the function carries on and int("") crashes.
3Checks run in order: existence, then type, then range, so the range check never sees non-numbers CExistence check that rejects empty input first by returning False
4One validation function can be reused for many different inputs Dis_valid_age("17") is_valid_age("abc") is_valid_age("")
5def is_valid_age(text): if 0 <= int(text) <= 120: return True if not text.isdigit(): return False Eif text == "": return False if not text.isdigit(): return False return 0 <= int(text) <= 120
6def is_valid_age(text): if text == "": print("Missing") return 0 <= int(text) <= 120 FThe range check runs before the type check, so int(text) crashes on input like "abc". The type check must come before the range check.

Definitions

def is_valid_age(text):

Fill in the Blanks

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

1.
if text __ "": return False
2.
if not text._______(): return False
3.
age = int(text) return age >= 0 ___ age <= 120
4.
if text == "": return _____, "Age is required"
5.
for text in ages: if ____________(text): print("Accepted")

Spot the Error

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

Question 1
A def is_valid_age(text):
B if text != "":
C return False
D if not text.isdigit():
E return False
F return int(text) <= 120
Question 2
A def is_valid_age(text):
B if text == "":
C return False
D if text.isdigit():
E return False
F return int(text) <= 120
Question 3
A def is_valid_age(text):
B if text.strip() = "":
C return False
D if not text.isdigit():
E return False
F return int(text) <= 120
Question 4
A def check_age(text):
B if len(text) == 1:
C return "Enter an age"
D if not text.isdigit():
E return "Not a number"
F return ""
Question 5
A def is_valid_mark(text):
B if not text.isdigit():
C return True
D mark = int(text)
E return 0 <= mark <= 100

Answer Key

Combining validation checks in a validation function
Section A: Quick Questions
Q1
A — Existence, then type, then range
Q2
A — True
Q3
B — if text == "": return False
Q4
B — Comparing text such as "abc" with a number would cause a runtime error
Q5
A — Put the checks in one function and call it for each input
Section B: Match the Pairs
Q1
def is_valid_age(text): → Defines a validation function that takes the user's text and will return True or False
Q2
if text == "": return False → Existence check that rejects empty input first by returning False
Q3
Checks run in order: existence, then type, then range, so the range check never sees non-numbers → if text == "": return False if not text.isdigit(): return False return 0 <= int(text) <= 120
Q4
One validation function can be reused for many different inputs → is_valid_age("17") is_valid_age("abc") is_valid_age("")
Q5
def is_valid_age(text): if 0 <= int(text) <= 120: return True if not text.isdigit(): return False → The range check runs before the type check, so int(text) crashes on input like "abc". The type check must come before the range check.
Q6
def is_valid_age(text): if text == "": print("Missing") return 0 <= int(text) <= 120 → The existence check only prints a message and does not return False, so the function carries on and int("") crashes.
Section C: Definitions
Q1
def is_valid_age(text): — A validation function that runs the existence, type and range checks in order and returns True or False, so the same checks can be reused for many inputs and tested.
Section D: Fill in the Blanks
Q1
if text == "": return False
Q2
if not text.isdigit(): return False
Q3
age = int(text) return age >= 0 and age <= 120
Q4
if text == "": return False, "Age is required"
Q5
for text in ages: if is_valid_age(text): print("Accepted")
Section E: Spot the Error
Q1
B — The existence check must catch an empty entry. Using != returns False for every non-empty value. It should be if text == "": so that only blank input is rejected.
Q2
D — The type check should reject text that is NOT made of digits, so it needs not. As written, valid numbers return False. It should be if not text.isdigit():.
Q3
B — A single = assigns a value and is a syntax error inside an if. A comparison needs ==, so it should be if text.strip() == "":.
Q4
B — The existence check should only trigger when nothing was entered. Testing for length 1 flags one-digit ages and misses the empty string. It should be if text == "": (or len(text) == 0).
Q5
C — When the type check fails the input is not a number, so the function must return False. Returning True would let invalid data through. It should be return False.