← VCE Data Analytics U4O1

Type checking in Python

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.

Understanding Type checking in Python in VCE Data Analytics U4O1

What it does: The isdigit() check is False for text like 'abc', so the program rejects it and shows an error message. If int(value) fails, Python raises a ValueError and the except block handles the invalid type. isdigit() is a method, so it needs brackets. It returns True if the string holds only digits. A type check asks whether the input is a number at all. A range check asks whether that number is within limits. Checking with isdigit() (or wrapping int() in try/except) stops bad input from crashing the program. Calling isdigit(age) causes a NameError. The correct type check is if age.isdigit():.

Common mistakes: The else keyword must be followed by a colon. The line should be else:. A try block starts with a colon, not a semicolon. The line should be try:.

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 63–83) and word search (Modules 63–105).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which check confirms that the string age is made up of digits only?
age.isdigit()age.isdigits
Matches
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.
Applying
Meanings
Term
isdigit()
Meaning
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.
Practice
age = "15"
Testing
Gaps
if age.():
Check Answer
Spots
3 print("Valid number")
4else

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

The 'Rhinoceros' badge — Thick-Skinned Coder, 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. Which check confirms that the string age is made up of digits only?
A. age.isdigits
B. age.isdigit() ✓
isdigit() is a method, so it needs brackets. It returns True if the string holds only digits.
2. What does "abc".isdigit() return?
A. True
B. False ✓
Letters are not digits, so isdigit() returns False. That is how text gets rejected where a number is needed.
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")
int("abc") raises a ValueError, so that is the error the except block must catch.
4. What happens when int("hello") runs without try/except?
A. The program crashes with a ValueError ✓
B. It returns 0 and carries on
int() cannot convert text to a number. Without try/except, Python stops with a ValueError.
5. Which input would "25".isdigit() accept as a valid number entry?
A. "25" (returns True) ✓
B. "2.5" (returns True)
The decimal point is not a digit, so "2.5".isdigit() returns False. Only "25" passes.

Match the Pairs

age.isdigit() → Returns True only if the string age contains digits only, so it passes as a whole number
int(value) → Tries to convert text to a whole number and raises ValueError if it cannot
Reject text where a number is needed by testing the string before using it → if not score.isdigit(): print("Enter a number")
Use try/except to catch input that cannot be converted to an integer → try: n = int(value) except ValueError: print("Not a number")
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.
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.

Key Term

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.

Fill in the Blanks

1. age = input("Age: ") if age.isdigit(): print("Age accepted")
Hint: Type the string method that returns True only when every character is a digit.
2. try: age = int(input("Age: ")) except ValueError:
Hint: Type the function that tries to convert text into a whole number.
3. try: score = int(entry) except ValueError:
Hint: Type the name of the error Python raises when int() is given text that is not a number.
4. if not age.isdigit(): print("Numbers only!")
Hint: Type the keyword that reverses True/False so that text is rejected.
5. try: num = int(value) except ValueError:
Hint: Type the keyword that catches the error when the type check fails.

Spot the Error

Question 1
A age = input("Age: ")
B if age.isdigit():
C print("Valid number")
D else
E print("Not a number")
Line D — The else keyword must be followed by a colon. The line should be else:
Question 2
A try;
B qty = int(input("Qty: "))
C except ValueError:
D print("Not a number")
Line A — A try block starts with a colon, not a semicolon. The line should be try:
Question 3
A value = int(input("Enter score: "))
B if value.isdigit():
C print("Accepted")
D else:
E print("Rejected")
Line 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.
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")
Line 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.
Question 5
A text = input("Age: ")
B if text.isdigit:
C print("Valid")
D else:
E print("Invalid")
Line 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():

Student with a class code? Join your class →