TypeError: 'int' object is not iterable
TypeError: 'NoneType' object is not subscriptable
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Youβre using a value in a way that doesnβt match its type. Python is telling you what type it got and what it expected.
Fix 1: Not Iterable
# β Trying to loop over a number
for i in 5: # π₯ int is not iterable
print(i)
# β
Use range()
for i in range(5):
print(i)
Fix 2: Not Subscriptable
# β Indexing into None
result = some_function() # Returns None
print(result[0]) # π₯ NoneType is not subscriptable
# β
Check for None first
result = some_function()
if result:
print(result[0])
Fix 3: Unsupported Operand
# β Adding string and int
age = 25
print("Age: " + age) # π₯ Can't add str and int
# β
Convert to string
print("Age: " + str(age))
# Or use f-string
print(f"Age: {age}")
Fix 4: Not Callable
# β Variable shadows a function name
list = [1, 2, 3] # Overwrites the built-in list()
new_list = list("hello") # π₯ list is now a variable, not a function
# β
Don't use built-in names as variables
my_list = [1, 2, 3]
Fix 5: Missing Arguments
# β Forgot self in class method
class User:
def greet(): # Missing self
print("Hello")
User().greet() # π₯ takes 0 positional arguments but 1 was given
# β
Add self
class User:
def greet(self):
print("Hello")
Fix 6: Wrong Return Type
# β Function returns None unexpectedly
def get_items():
items = [1, 2, 3]
items.sort() # sort() returns None!
result = get_items()
for item in result: # π₯ NoneType is not iterable
# β
Return the list explicitly
def get_items():
items = [1, 2, 3]
items.sort()
return items
Related resources
- Python int not iterable fix
- Python NoneType not subscriptable fix
- Python str not callable fix
- Python cheat sheet
Related: Pip Install Error Fix