Type checking in code

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

Type checking in code

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A student types 42 into input(). What data type is the value that input() returns?
A An integer
B A string (text)
2. Which line correctly checks that the string age contains only whole-number digits?
A age.isdigit()
B isdigit(age)
3. What does "3.5".isdigit() return?
A False, because the . is not a digit
B True, because 3.5 is a number
4. Which error does int("abc") raise, which a type check should catch?
A except IndexError:
B except ValueError:
5. Which approach lets you check that input is a valid decimal number such as 3.5?
A try: float(text) with except ValueError
B text.isdigit()

Match the Pairs

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

Left columnRight column
1age.isdigit() Ainput() always returns text (str), so isinstance(age, int) is never True. Use age.isdigit() on the text, or try int(age), instead.
2isinstance(price, float) Btry: h = float(text) except ValueError: h = None
3Test whether text is a whole number by trying to convert it and catching the failure Ctry: n = int(text) except ValueError: print("Not a whole number")
4Check that input is a decimal number by trying float() on the text Disdigit() is False for decimals and negatives because '.' and '-' are not digits. Use try/except around float(temp) to check for a decimal number.
5age = input("Age: ") if isinstance(age, int): print("Valid") EReturns True if the value stored in price is already a float
6temp = "-4.5" if temp.isdigit(): print("Valid number") FReturns True if the text contains only digits, so it is a safe whole number to convert with int()

Definitions

type check

Fill in the Blanks

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

1.
age = input("Age: ") if age._______(): age = int(age)
2.
try: n = ___(text) except ValueError:
3.
try: price = float(text) except __________:
4.
value = 3.5 print(isinstance(value, _____))
5.
num = input("Number: ") print(____(num))

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(int(age) + 1)
Question 2
A num = input("Number: ")
B if isinstance(num, int):
C num = int(num)
D print(num * 2)
Question 3
A try:
B n = int(input("Num: "))
C except TypeError:
D print("Not a whole number")
Question 4
A val = float(input("Price: "))
B try:
C price = float(val)
D except ValueError:
E print("Not a number")
Question 5
A qty = input("Quantity: ")
B if qty.isdigit():
C total = qty * 5
D else:
E print("Whole numbers only")

Answer Key

Type checking in code
Section A: Quick Questions
Q1
B — A string (text)
Q2
A — age.isdigit()
Q3
A — False, because the . is not a digit
Q4
B — except ValueError:
Q5
A — try: float(text) with except ValueError
Section B: Match the Pairs
Q1
age.isdigit() → Returns True if the text contains only digits, so it is a safe whole number to convert with int()
Q2
isinstance(price, float) → Returns True if the value stored in price is already a float
Q3
Test whether text is a whole number by trying to convert it and catching the failure → try: n = int(text) except ValueError: print("Not a whole number")
Q4
Check that input is a decimal number by trying float() on the text → try: h = float(text) except ValueError: h = None
Q5
age = input("Age: ") if isinstance(age, int): print("Valid") → input() always returns text (str), so isinstance(age, int) is never True. Use age.isdigit() on the text, or try int(age), instead.
Q6
temp = "-4.5" if temp.isdigit(): print("Valid number") → isdigit() is False for decimals and negatives because '.' and '-' are not digits. Use try/except around float(temp) to check for a decimal number.
Section C: Definitions
Q1
type check — A check that input is the expected data type, using .isdigit(), try/except around int() or float(), or isinstance(). input() is always text, so check before converting.
Section D: Fill in the Blanks
Q1
age = input("Age: ") if age.isdigit(): age = int(age)
Q2
try: n = int(text) except ValueError:
Q3
try: price = float(text) except ValueError:
Q4
value = 3.5 print(isinstance(value, float))
Q5
num = input("Number: ") print(type(num))
Section E: Spot the Error
Q1
B — isdigit is a method, so it needs brackets to be called. Without them the condition is just the method object, which is always True, so nothing is checked. It should be if age.isdigit():
Q2
B — input() always returns a string, so isinstance(num, int) is always False. To check that the text is a whole number, use num.isdigit().
Q3
C — int("abc") raises a ValueError, not a TypeError, so this except would not catch bad input and the program would crash. It should be except ValueError:
Q4
A — The float() conversion happens before the try block, so bad input crashes the program before it can be caught. Keep input() as text on line 1 and convert inside the try: val = input("Price: ")
Q5
C — qty is still text after the isdigit() check. Multiplying a string by 5 repeats it ("3" becomes "33333") instead of doing maths. Convert it first: total = int(qty) * 5