← VCE Software Development U3O1

Type checking in code

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.

Understanding Type checking in code in VCE Software Development U3O1

What it does: If int() cannot convert the text it raises a ValueError, and the except block handles it. This is a type check using try/except. float() accepts values like 1.75 that isdigit() would reject, so try/except around float() checks for a decimal number. input() always returns text, even when the user types digits. That is why we check the value before converting it. isdigit() is a string method, so it is called with dot notation on the string: age.isdigit(). isinstance() checks the actual data type of a value against a type such as float, int or str.

Common mistakes: 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():. input() always returns a string, so isinstance(num, int) is always False. To check that the text is a whole number, use num.isdigit().

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 114–124) and word search (Modules 114–141).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A student types 42 into input(). What data type is the value that input() returns?
A string (text)An integer
Matches
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.
Applying
Meanings
Term
type check
Meaning
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.
Practice
value = "42a"
Testing
Gaps
if age.():
Check Answer
Spots
1age = input("Age: ")
2if age.isdigit:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Mantis Shrimp' badge — Speed Striker, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A student types 42 into input(). What data type is the value that input() returns?
A. An integer
B. A string (text) ✓
input() always returns text, even when the user types digits. That is why we check the value before converting it.
2. Which line correctly checks that the string age contains only whole-number digits?
A. age.isdigit() ✓
B. isdigit(age)
isdigit() is a string method, so it is called with dot notation on the string: age.isdigit().
3. What does "3.5".isdigit() return?
A. False, because the . is not a digit ✓
B. True, because 3.5 is a number
isdigit() only accepts the characters 0-9. The decimal point makes it return False, so it can't check for decimal numbers.
4. Which error does int("abc") raise, which a type check should catch?
A. except IndexError:
B. except ValueError: ✓
int() raises a ValueError when the text can't be converted to a whole number. IndexError is for list positions.
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()
float() accepts decimals, so wrapping it in try/except ValueError confirms the text is a valid decimal number. isdigit() rejects the decimal point.

Match the Pairs

age.isdigit() → Returns True if the text contains only digits, so it is a safe whole number to convert with int()
isinstance(price, float) → Returns True if the value stored in price is already a float
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")
Check that input is a decimal number by trying float() on the text → try: h = float(text) except ValueError: h = None
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.
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.

Key Term

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.

Fill in the Blanks

1. age = input("Age: ") if age.isdigit(): age = int(age)
Hint: Type the string method that checks every character is a digit
2. try: n = int(text) except ValueError:
Hint: Type the conversion function for whole numbers
3. try: price = float(text) except ValueError:
Hint: Type the error name raised when text cannot be converted to a number
4. value = 3.5 print(isinstance(value, float))
Hint: Type the data type name for a decimal number
5. num = input("Number: ") print(type(num))
Hint: Type the built-in function that reports a value's data type

Spot the Error

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

Student with a class code? Join your class →