Common Python Errors (By Python Data Development 2023)
Common Python Errors (By Python Data Development 2023)
Save
It is inevitable to encounter various errors when using Python. Here are some
common Python errors and their solutions.
2. The .pyc file has cached code that has not been updated. Solution: Delete the
.pyc file for that library.
1. The Python built-in object does not have the attribute. Solution: Remove the
relevant access
2. The Python custom object has not set the attribute. Solution: Add the attribute
to the custom object
3. Accessing instance attributes through the class name. Solution: Modify to access
through the instance name.
1. Different indentation rules are used for code blocks at the same level (code is
not aligned). Solution: Use the key to align the code
1. The index value specified when getting the element exceeds the length of the
list. Solution: Modify the index value
2. The list is empty, and an error occurs when getting the element. Solution: Check
if it is not empty before getting it.
2. When getting a character from a string based on an index, the specified index
does not exist. Solution: Modify the index value or first check if it exists.
2. Forgetting to write the file extension. Solution: Complete the file name.
ITypeError: init()takes 0 positional arguments but 1 was given Description:
Incorrect number of positional arguments. Possible causes: The self argument
was not added to the init() method. Solution: Add the self argument.
10、KeyError: ‘age’
Description: Key error. Possible causes:
1. When getting the value of a dictionary based on a key, the specified key does not
exist. Solution: Modify to an existing key
3. The third-party module is not downloaded. Solution: Use pip to install the
required module.
1. Forgetting to write the end quotes. Solution: Add the missing quotes
3. Quotation nesting error. Solution: It is recommended to use single quotes for the
outer layer and double quotes for the inner layer.
1. Forgetting to write a colon after an if, while, for, etc. statement, or writing a
colon as a semicolon or other symbol. Solution: Change to an English half-width
colon.
The default value parameters are not placed at the end of all parameters.
3. Full-width spaces are included in the code line. Solution: Remove or modify to a
half-width space.
2. Request URL timeout. Solution: Check if the URL is correct and can be accessed
1. int() function received a string type data that is not a number. Solution: Modify
the non-numeric string to a numeric string.