Type checking in Python

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

Type checking in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which check confirms that the string age is made up of digits only?
A age.isdigits
B age.isdigit()
2. What does "abc".isdigit() return?
A True
B False
3. Which code is a type check using try/except?
A try: n = int(value) except ValueError: print("Not a number")
B try: n = int(value) except NameError: print("Not a number")
4. What happens when int("hello") runs without try/except?
A The program crashes with a ValueError
B It returns 0 and carries on
5. Which input would "25".isdigit() accept as a valid number entry?
A "25" (returns True)
B "2.5" (returns True)

Match the Pairs

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

Left columnRight column
1age.isdigit() AReturns True only if the string age contains digits only, so it passes as a whole number
2int(value) Bif not score.isdigit(): print("Enter a number")
3Reject text where a number is needed by testing the string before using it CThe except block has no try before it, so int(value) is not protected and the code is a syntax error.
4Use try/except to catch input that cannot be converted to an integer Disdigit() is a string method, so it must be called on the string as age.isdigit(), not as a stand-alone function.
5if isdigit(age): ETries to convert text to a whole number and raises ValueError if it cannot
6n = int(value) except ValueError: Ftry: n = int(value) except ValueError: print("Not a number")

Definitions

isdigit()

Fill in the Blanks

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

1.
age = input("Age: ") if age._______(): print("Age accepted")
2.
try: age = ___(input("Age: ")) except ValueError:
3.
try: score = int(entry) except __________:
4.
if ___ age.isdigit(): print("Numbers only!")
5.
try: num = int(value) ______ ValueError:

Spot the Error

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

Question 1
A age = input("Age: ")
B if age.isdigit():
C print("Valid number")
D else
E print("Not a number")
Question 2
A try;
B qty = int(input("Qty: "))
C except ValueError:
D print("Not a number")
Question 3
A value = int(input("Enter score: "))
B if value.isdigit():
C print("Accepted")
D else:
E print("Rejected")
Question 4
A text = input("Quantity: ")
B if text.isdigit():
C qty = int(text)
D print("Total:", text * 2)
E else:
F print("Digits only")
Question 5
A text = input("Age: ")
B if text.isdigit:
C print("Valid")
D else:
E print("Invalid")

Answer Key

Type checking in Python
Section A: Quick Questions
Q1
B — age.isdigit()
Q2
B — False
Q3
A — try: n = int(value) except ValueError: print("Not a number")
Q4
A — The program crashes with a ValueError
Q5
A — "25" (returns True)
Section B: Match the Pairs
Q1
age.isdigit() → Returns True only if the string age contains digits only, so it passes as a whole number
Q2
int(value) → Tries to convert text to a whole number and raises ValueError if it cannot
Q3
Reject text where a number is needed by testing the string before using it → if not score.isdigit(): print("Enter a number")
Q4
Use try/except to catch input that cannot be converted to an integer → try: n = int(value) except ValueError: print("Not a number")
Q5
if isdigit(age): → isdigit() is a string method, so it must be called on the string as age.isdigit(), not as a stand-alone function.
Q6
n = int(value) except ValueError: → The except block has no try before it, so int(value) is not protected and the code is a syntax error.
Section C: Definitions
Q1
isdigit() — A type check method: returns True if a string holds only digits, so text can be rejected where a number is needed. The alternative is to put int(value) inside try/except.
Section D: Fill in the Blanks
Q1
age = input("Age: ") if age.isdigit(): print("Age accepted")
Q2
try: age = int(input("Age: ")) except ValueError:
Q3
try: score = int(entry) except ValueError:
Q4
if not age.isdigit(): print("Numbers only!")
Q5
try: num = int(value) except ValueError:
Section E: Spot the Error
Q1
D — The else keyword must be followed by a colon. The line should be else:
Q2
A — A try block starts with a colon, not a semicolon. The line should be try:
Q3
A — isdigit() only works on strings. Converting the input to an int first would crash on the next line. It should be value = input("Enter score: ") so the text can be checked first.
Q4
D — text is still a string, so text * 2 repeats the text instead of doubling the number. It should use the converted number: qty * 2.
Q5
B — isdigit is a method and needs brackets to run. Without them the condition is always true, so text is never rejected. It should be if text.isdigit():