Runtime error: type mismatch

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

Runtime error: type mismatch

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What happens when Python runs: print('5' + 3)?
A TypeError: it cannot add a string and an integer
B It prints 8
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
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: '))
4. Which is a type mismatch (TypeError) in Python?
A 'Score: ' + 10
B '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))

Match the Pairs

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

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

Definitions

TypeError

Fill in the Blanks

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

1.
total = ___(input("Enter a number: ")) + 5
2.
print("Score: " + ___(score))
3.
try: total = '5' + 3 except _________:
4.
num = int(_____("Enter a number: "))
5.
age = input("Age: ") print(____(age))

Spot the Error

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

Question 1
A age = int(input("Age: "))
B years = 5
C total = age + str(years)
D print(total)
Question 2
A name = input("Name: ")
B score = 7
C msg = name + score
D print(msg)
Question 3
A num = input("Number: ")
B result = num + 10
C print(result)
Question 4
A count = 3
B label = "Items: " + count
C print(label)
Question 5
A a = input("First: ")
B b = input("Second: ")
C total = int(a) + b
D print(total)

Answer Key

Runtime error: type mismatch
Section A: Quick Questions
Q1
A — TypeError: it cannot add a string and an integer
Q2
A — input() returns a string, so age + 1 mixes str and int
Q3
B — age = int(input('Age: '))
Q4
A — 'Score: ' + 10
Q5
B — print('Total: ' + str(total))
Section B: Match the Pairs
Q1
'5' + 3 → Raises a TypeError because a string cannot be added to an integer
Q2
int('5') + 3 → Converts the string to an integer first, so the answer is 8
Q3
input() always returns a string, so convert it with int() before doing maths → age = int(input('Age: ')) + 1
Q4
To join a number to text, convert the number to a string with str() → print('Score: ' + str(10))
Q5
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: ')).
Q6
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'.
Section C: Definitions
Q1
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()).
Section D: Fill in the Blanks
Q1
total = int(input("Enter a number: ")) + 5
Q2
print("Score: " + str(score))
Q3
try: total = '5' + 3 except TypeError:
Q4
num = int(input("Enter a number: "))
Q5
age = input("Age: ") print(type(age))
Section E: Spot the Error
Q1
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.
Q2
C — name is a string and score is an integer, so + causes a TypeError. Convert the number with str(): msg = name + str(score).
Q3
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: ")).
Q4
B — You cannot join a string and an integer with +. This causes a TypeError. Convert the integer first: "Items: " + str(count).
Q5
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: ")).