AttributeErrorWhat is an AttributeError?
An AttributeError means you used a method or attribute that this kind of value does not have, often on None or with a name from another language.
What AttributeError means
AttributeError: 'NoneType' object has no attribute 'append'The part after the dot, like .append or .upper, is an attribute. Each type has its own set. The message names the type you actually had and the attribute it does not have.
The usual causes:
- A
Noneyou did not expect.items = items.sort()setsitemstoNone, becausesortchanges the list and returns nothing. - A method borrowed from another language. Lists have
append, notpush. - The wrong type. Calling
.upper()on a number, or.append()on a string.
How to find it
Read the type in quotes first. If it says NoneType, look for the line that gave that name its value, usually an assignment from a method that returns nothing. Otherwise check the attribute's spelling and whether this type has it: dir(x) lists them all.
Run it: push is not a list method
Press Run and read the error from the bottom up.
queue = ["Ana", "Ben"]
queue.push("Cleo")
print(queue)
The sorted playlist that vanishes
This should sort the playlist, add a bonus track at the end, and print it. Fix it.
playlist = ["Static", "Intro", "Neon Rain"]
playlist = playlist.sort()
playlist.append("Bonus")
print(playlist)