Python error guide
The errors beginners meet most often, what they mean, and how to fix them. In Py-CRAFT's editor, each of these is also explained automatically when it happens in your own code.
SyntaxError
- SyntaxErrorThe code breaks Python's grammar rules, so Python cannot even start running it.
- SyntaxError: unterminated string literalA piece of text was opened with a quote but never closed on the same line.
- SyntaxError: expected ':'A line that starts a block is missing the colon at the end.
- SyntaxError: '(' was never closedAn opening bracket has no matching closing bracket.
- SyntaxError: Missing parentheses in call to 'print'You wrote
printthe old Python 2 way. - SyntaxError: invalid decimal literalA name was written starting with a digit.
IndentationError
- IndentationErrorThe spaces at the start of a line are wrong for the structure of the code.
- IndentationError: unexpected indentA line is indented when nothing above it opened a block.
- IndentationError: unindent does not matchA line steps back to a position that no earlier line used.
NameError
- NameErrorPython met a name (a variable or function) that it does not know.
- NameError: name 'math' is not definedYou used the math module without importing it first.
- NameError: name 'np' is not definedYou used
npwithout importing NumPy under that name.
TypeError
- TypeErrorAn operation was used with a type of value it does not work with, such as adding text and a number.
- TypeError: 'int' object is not callableYou put brackets after something that is not a function, so Python tried to call it and could not.
- TypeError: 'int' object is not subscriptableYou used square brackets on a value that has no items to pick from.
- TypeError: 'int' object is not iterableA loop or function expected something with several items, but it was given a single number.
- TypeError: missing 1 required positional argumentYou called a function with fewer values than it needs.
- TypeError: takes 1 positional argument but 2 were givenYou called a function with more values than it has parameters.
- TypeError: '>' not supported between instances of 'str' and 'int'You compared text with a number.
- TypeError: 'float' object cannot be interpreted as an integer
range()was given a decimal number, but it needs a whole number. - TypeError: unhashable type: 'list'You used a list where Python needs a value that cannot change, such as a dictionary key.
- TypeError: string indices must be integersYou used text where a whole-number position was needed to pick a character.
- TypeError: unsupported operand type(s) for +: 'int' and 'str'You used an operator with two kinds of value that Python cannot combine.
ValueError
- ValueErrorThe type of value is right, but the value itself cannot be used.
- ValueError: too many values to unpack (expected 2)You tried to split a group of values into fewer names than there are values.
- ValueError: not enough values to unpack (expected 3, got 2)You gave fewer values than there are names to fill.
- ValueError: math domain errorYou gave a maths function a value it cannot work with.
IndexError
- IndexErrorYou asked for a position in a list (or string) that does not exist.
- IndexError: string index out of rangeYou asked for a character at a position the text does not have.
KeyError
AttributeError
- AttributeErrorYou used a method or attribute that this kind of value does not have.
- AttributeError: 'NoneType' object has no attribute 'append'A variable holds None, which means nothing, so the method you called does not exist.
- AttributeError: 'str' object has no attribute 'append'You used a list method on text.
NumPy errors
- ValueError: operands could not be broadcast togetherNumPy cannot combine two arrays because their shapes do not fit together.
- ValueError: truth value of an array is ambiguousAn
iforandwas given a whole array of True and False values, and Python cannot decide which one to use. - ValueError: cannot reshape array of size 6 into shape (4,2)The new shape needs a different number of elements than the array has.
- IndexError: index 3 is out of bounds for axis 0 with size 3You asked for a position that the array does not have.
Other errors
- ZeroDivisionErrorA number was divided by zero, which has no answer.
- ModuleNotFoundErrorPython could not find the module you tried to import.
- UnboundLocalErrorA function used a variable that it also assigns to, before assigning it.
- RecursionErrorA function called itself too many times without stopping.
- EOFErrorThe program called
input()but there was no more input to read. - FileNotFoundErrorThe program tried to open a file that does not exist.
- Timeout (endless loop)The program kept running without finishing, so Py-CRAFT stopped it after a few seconds.
How to use this guide
Each page shows a short program that causes the error, the exact message Python gives, why it happens, and a corrected version that you can open in the editor. Read the message from the bottom: the last line names the kind of error, and the line number above it shows where it happened. Most beginner errors fall into a handful of types, so learning these 44 will help with almost everything you meet.