← VCE Software Development U3O1

Runtime error: type mismatch

A runtime error when an operation gets the wrong data types, like '5' + 3 (string plus integer). Fix it by converting with int() or str(), e.g. int(input()).

Understanding Runtime error: type mismatch in VCE Software Development U3O1

What it does: Wrapping input() in int() makes the value a number, so adding 1 avoids a TypeError. str(10) gives the string '10', so both sides of + are strings and the TypeError is avoided. '5' is a string and 3 is an integer. Python will not mix them with +, so it raises a TypeError at runtime. int() turns the typed text into a whole number so you can do maths with it. str() keeps it as text, so the error remains. The code '5' + 3 is valid syntax. Python only discovers the mismatch when it tries to run that line, so it is a runtime error.

Common mistakes: age is an integer, but str(years) is a string. Adding an int to a str raises a TypeError. Both values must be numbers: total = age + years. name is a string and score is an integer, so + causes a TypeError. Convert the number with str(): msg = name + str(score).

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 156–166) and word search (Modules 142–166).

Take this worksheet further — free with Cloodit

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

Learning
Duo
What happens when Python runs: print('5' + 3)?
It prints 8TypeError: it cannot add a string and an integer
Matches
total = input('Qty: ') * 2.5
↓
input() returns a string, so multiplying it by a float causes a TypeError. It needs float(input('Qty: ')) or int(input('Qty: ')).
Applying
Meanings
Term
TypeError
Meaning
A runtime error when an operation gets the wrong data types, like '5' + 3 (string plus integer). Fix it by converting with int() or str(), e.g. int(input()).
Practice
age = input("Enter your age: ")
Testing
Gaps
total = (input("Enter a number: ")) + 5
Check Answer
Spots
2years = 5
3total = age + str(years)

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

The 'Seahorse' badge — Range Rider, 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. What happens when Python runs: print('5' + 3)?
A. TypeError: it cannot add a string and an integer ✓
B. It prints 8
'5' is a string and 3 is an integer. Python will not mix them with +, so it raises a TypeError at runtime.
2. age = input('Age: ') print(age + 1) Why does this crash?
A. input() returns a string, so age + 1 mixes str and int ✓
B. The variable name age is not allowed
input() always gives back a string. Adding 1 to it causes a TypeError unless you convert it first.
3. Which line correctly fixes the crash when adding 1 to the user's typed age?
A. age = str(input('Age: '))
B. age = int(input('Age: ')) ✓
int() turns the typed text into a whole number so you can do maths with it. str() keeps it as text, so the error remains.
4. Which is a type mismatch (TypeError) in Python?
A. 'Score: ' + 10 ✓
B. 'Score: ' + '10'
Adding a string and an integer causes a TypeError. Joining two strings is fine and gives 'Score: 10'.
5. You want to print 'Total: ' followed by the integer total. Which works without a TypeError?
A. print('Total: ' + total)
B. print('Total: ' + str(total)) ✓
str(total) converts the integer to a string so it can be joined to other text with +.

Match the Pairs

'5' + 3 → Raises a TypeError because a string cannot be added to an integer
int('5') + 3 → Converts the string to an integer first, so the answer is 8
input() always returns a string, so convert it with int() before doing maths → age = int(input('Age: ')) + 1
To join a number to text, convert the number to a string with str() → print('Score: ' + str(10))
total = input('Qty: ') * 2.5 → input() returns a string, so multiplying it by a float causes a TypeError. It needs float(input('Qty: ')) or int(input('Qty: ')).
print('You are ' + 16 + ' years old') → The integer 16 is added to strings without conversion, causing a TypeError. It should be str(16) or the number should be written as '16'.

Key Term

TypeError
A runtime error when an operation gets the wrong data types, like '5' + 3 (string plus integer). Fix it by converting with int() or str(), e.g. int(input()).

Fill in the Blanks

1. total = int(input("Enter a number: ")) + 5
Hint: Type the function that converts the text from input() into a whole number.
2. print("Score: " + str(score))
Hint: Type the function that converts a number into text so it can be joined to a string.
3. try: total = '5' + 3 except TypeError:
Hint: Type the name of the error Python raises when an operation gets the wrong data types.
4. num = int(input("Enter a number: "))
Hint: Type the function that reads text typed by the user.
5. age = input("Age: ") print(type(age))
Hint: Type the function that reveals the data type of a value, to check whether it is still text.

Spot the Error

Question 1
A age = int(input("Age: "))
B years = 5
C total = age + str(years)
D print(total)
Line C — age is an integer, but str(years) is a string. Adding an int to a str raises a TypeError. Both values must be numbers: total = age + years.
Question 2
A name = input("Name: ")
B score = 7
C msg = name + score
D print(msg)
Line C — name is a string and score is an integer, so + causes a TypeError. Convert the number with str(): msg = name + str(score).
Question 3
A num = input("Number: ")
B result = num + 10
C print(result)
Line A — input() always returns a string, so num + 10 on the next line adds a string to an integer and raises a TypeError. Wrap it: int(input("Number: ")).
Question 4
A count = 3
B label = "Items: " + count
C print(label)
Line B — You cannot join a string and an integer with +. This causes a TypeError. Convert the integer first: "Items: " + str(count).
Question 5
A a = input("First: ")
B b = input("Second: ")
C total = int(a) + b
D print(total)
Line B — b is left as a string because input() was not converted. Then int(a) + b adds an integer to a string and raises a TypeError. Use int(input("Second: ")).

Student with a class code? Join your class →