The following is a list of common errors in Python, and knowing how to fix them will save your day.
1. IndentationError
In Python, all code is lined up with correct spaces. So, whether there are extra spaces or missing spaces, the entire code doesn’t run and just returns an error function.
Python code follows the PEP8 whitespace specification, using 4 spaces for each level of indentation.
example:
how to fix:
2. Mixing Tab and Space (TabError)
This type of error is caused by using both tabs and spaces for encoding. The tab key is essentially a tab, not an indent. Spaces are recommended due to the varying width of spaces represented by tabs in different text editors.
3. SyntaxError
There are three reasons for syntax errors:
- invalid syntax
Missing punctuation, mixed use of Chinese and English symbols, misspelling, variable name or function name using keywords.
- Invalid character in identifier
Unrecognized characters appear in the code, check for extra characters or Chinese characters.
Incomplete string detected (EOL while scanning string literal)
In many cases, it is due to inconsistent quotation marks around the string.
some examples:
Error reason: the comma is a Chinese comma
Error message: SyntaxError: invalid character in identifier
Error reason: parentheses are not paired
Error message: SyntaxError:unexpected EOF while parsing
Cause of error: Forgot to add a colon at the end of statements such as if/elif/else/while/for/def/class
Error message: SyntaxError: invalid syntax
4. NameError
Variable name error is the most common and most commonly encountered built-in error type. It often occurs in Python variable naming. If the variable cannot be found, a NameError will be raised. There are a few things to keep in mind about the rules for variable names:
- The variable name can only contain letters, numbers and underscores, and cannot start with numbers;
- Variable names cannot contain spaces, but underscores can be used to separate words within them;
- Do not use Python keywords and function names as variable names, such as print;
- Variable names should be short and descriptive;
- Use lowercase l and uppercase O with caution, because it is easy to be mistaken for numbers 1 and 0.
If there is a variable name error, you can check whether the variable is assigned a value, whether there is a case of inconsistent capitalization or a wrong variable name, and correct it after finding it.
5. IndexError
An index is the position of an item in an array or list, this exception occurs when we try to access an element from a list or a tuple from an index that does not exist in the list.
For example, if you have a list of 10 elements with indices between 0 and 9, if you try to access an element at index 10 or 11 or more, an IndexError will be raised.
6. KeyError
When reading the key and value in the dictionary, if the key does not exist, a KeyError will be triggered.
7. TypeError
This error is raised when an incorrect or unsupported object type is used in a program. This error is also raised if you try to call a non-callable object or iterate through a non-iterable identifier.
example
Reason for error: When using “+” for splicing, you must use a string, or use the str() function to convert the number into a string
Error message: TypeError: can only concatenate str(not “int”) to str
8. AttributeError
Property errors are raised when attribute references and assignments fail.
The cause of such errors is an attempt to access an unknown object property, in other words the property of the corresponding object cannot be found. You can check whether the constructor init() in the class is written correctly, with two underscores on the left and right sides.