Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right 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. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
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.if text == "":
return Falseif not text.isdigit():
return Falseage = int(text)
return age >= 0 and age <= 120if text == "":
return False, "Age is required"for text in ages:
if is_valid_age(text):
print("Accepted")