Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / Data Types and Casting

Fix type errors

All topicsPractise exam questions
Knowledge organiser

Data Types and Casting

301.4d

Correct invalid type usage.

What you need to know

A TypeError happens when you use an operator on incompatible types, such as adding a string and an integer. The fix is to cast one value to match the other.

Key points

  • "5" + 3 causes TypeError — can't add str and int
  • Fix: int("5") + 3 or "5" + str(3)
  • input() always returns str — cast before maths
  • Read the error message to find the mismatched types

Code examples

Fixing a type error
python
age = input("Age: ")  # returns "15" (str)
# age + 1  ← TypeError!
age = int(age)
print(age + 1)  # 16