Python error explained
AttributeError: what it means and how to fix it
You used a method on something that does not have it, very often None.
The message
AttributeError: 'NoneType' object has no attribute 'append'
What it means
An AttributeError means the object exists but does not have that method or attribute. When the object is None, it usually means a function or method returned nothing and you stored that nothing. list.sort() and list.append() change the list in place and return None.
Common causes
- Writing items = items.sort() or items = items.append(x)
- A function that forgets to return its result
- A typo in a method name, like .apend()
- Calling a string method on a number
Example that raises it
scores = [42, 17, 88]
scores = scores.append(65)
scores.append(30)
print(scores)
The fix
scores = [42, 17, 88]
scores.append(65)
scores.append(30)
print(scores)
Tip: Methods that change a list in place return None. Call them on their own line, or use sorted(items) when you want a new sorted list back.
Fix the broken version yourself in the editor on this page. It runs real Python in your browser.