Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common mistakes in python and how to solve them?

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what common errors reported in python and how to solve the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this python common mistakes and how to solve the article will have a harvest, let's take a look.

AttribteError: 'module' object has no attribute xxx'

Description: the module has no related attributes. Possible reasons:

1. The .py file is named with the Python reserved word or the same as the module name.

Solution: modify file name

Code that is not updated is cached in the 2.pyc file.

Solution: delete the. pyc file of the library

AttributeError: 'Obj' object has no attribute' attr'

Description: the object has no related properties. Possible reasons:

The 1.Python built-in object has no properties.

Solution: remove related access

The 2.Python custom object has no properties set.

Resolution: adding attributes to custom objects

3. Access instance properties through the class name.

Solution: modified to access through instance name

FileExistsError: [Errmo 17] File exists

Description: the file already exists.

Solution: first determine whether the file exists, and if it already exists, do not recreate it

FileNotFoundError: [Ermo 2] No such file or directory

Description: the requested file or directory does not exist.

Solution: check that the path to the file or directory is correct

IndentationError: expected an indented block

Description: expect an indented block. Possible reasons:

1. There is no indentation when defining a structure under a function or class.

Resolution: adding indent

The structure under the 2.if or for or while statements is not indented.

Resolution: adding indent

IndentationError: unexpected indent

Description: indent error. Possible reasons:

A space appears in front of the code except for indentation.

Solution: delete extra spaces

IndentationError: unindent does not match any outer indentation level

Description: occupancy problem. Possible reasons:

1. Blocks of code at the same level have different indentation rules (the code is not aligned).

Solution: align with key

two。 There is invisible content in illegal characters or other formats.

Solution: remove illegal characters

IndexError: list index out of range

Description: list subscript out of bounds, possible reasons

1. The index value specified when getting the element exceeds the length of the list.

Resolution: modifying index valu

two。 The list is empty and an error will occur when getting the element.

Solution: judge that it is not empty and get it again

IndexError: string index out of range

Description: string subscript out of bounds, possible reasons

1. When intercepting a string, the specified index does not exist.

Resolution: modifying index valu

two。 When you get a character in a string based on the index, the specified index does not exist.

Solution: modify the index value or make a judgment first

SADirectoryError: [Ermo 21] Is a directory

Description: wanted to manipulate the file, but provided a directory error. Possible reasons:

1. Treat a directory as a file, for example, test is a directory, and an error is raised when using os.remove (test).

Solution: add the corresponding file name

two。 Forgot to write the file extension.

Solution: complete the file name

ITypeError: _ init _ () takes 0 positional arguments but 1 was given

Description: the number of location parameters is incorrect. Possible reasons:

The self parameter is not added to the _-init__ () method.

Solution: add self parameter

KeyError: 'age'

Description: key error. Possible reasons:

1. When getting the value of the dictionary based on the key, the specified key does not exist.

Solution: change to existing key

two。 Delete a key that does not exist.

Solution: determine whether it exists before deletion

ModuleNotFoundError: No module named' pymysql'

Description: module does not exist, possible reasons:

1. The module name is misspelled.

Solution: correct modification

two。 There is no module introduced.

Solution: import the module using the import statement

3. The third-party module was not downloaded.

Solution: install the required modules using pip

NameError:name test' is not defined

Description: a local or global variable name was not found. Possible reasons:

1. The variable is not defined.

Solving: defining variabl

The 2.Python3 version does not support some functions or methods in Python2, such as xrange ().

Solution: modify to a function or method in Python3

RecursionEror:maximum rcusion depth exceded in comprison

Description: exceeds the maximum recursive depth

Solution: disable infinite recursion, set recursion depth

SyntaxError: EOL whilescanning string literal

Description: quotation marks for strings do not appear in pairs. What may happen:

1. Forget to write the closing quotation marks.

Solution: add missing quotation marks

two。 Include\ at the end of the string.

Solution: replace\ with\ or /

3. Incorrect nesting of quotation marks.

Solution: it is recommended to use outer single quotation marks and inner double quotation marks

SyntaxError: invalid syntax

Description: invalid syntax. Possible reasons:

1. Forget to write colons after if, while, for, etc., or write semicolons and other symbols.

Solution: changed to English half-corner colon

two。 The parentheses of a function or method are written as

Solution: change it to English half-corner ()

3. An equal sign = appears in the conditional expression.

Solution: change it to the comparison operator = =

4. Functions with no arguments are defined without parentheses.

Resolution: adding parentheses

Syntax Error: non-default argument follows default argument

Description: parameter definition is in the wrong order.

What may happen: the default parameter is not placed at the end of all parameters.

Resolution: changing the order of parameters

SyntaxError: invalid character in identifier

Description: invalid characters appear in the identifier.

Possible reasons:

1. The parentheses of a function or method are entered in Chinese.

Solution: change to input in English half-angle state

two。 The string delimiter uses Chinese quotation marks.

Solution: change to input in English half-angle state

3. The line of code includes full-width spaces.

Solve: remove or modify to half-width spaces

TimeouEror [WinEror 10060 the connection attempt failed because the connecting party did not reply correctly or the connected host did not respond after a period of time.

Description: connection timed out. Possible reasons:

1. The database connection timed out.

Solution: check whether the database connection configuration information is correct

two。 Request URL timed out.

Solution: check whether the URL is correct and can be accessed properly

3. Connection to the FTP service timed out.

Solution: check whether the parameters of the connection FTP are correct

TypeError: can only concatenate str (not "XXX") to str

Description: strings can only be concatenated with strings.

Possible reason: concatenate strings with data of non-string types (such as integers, floats, Boolean, sequence objects, and so on) using +.

Solution: use the str () function to convert and then connect

TypeError: F () takes exactly 2 arguments (1 given)

Description: the number of parameters provided for the function is not enough.

What may happen: define two parameters and pass only one of them when called.

Solution: complete the parameters

TypeError: 'tuple' object does not support item assignment

Description: tuple elements cannot be modified.

Possible reason: get the tuple element through the index and modify it.

Solution: change the tuple to a list or delete the modify operation

ValueError: could not convert string to float:' 12.2'

Description: cannot convert a string to a floating point number.

Possible reason: the float () function accepts string type data that is not a floating point number.

Solution: change to floating point numeric string

ValueError: invalid literal for int () with base 10

Description: invalid parameter passed in.

Possible reasons:

The 1.int () function accepts non-numeric string type data.

Solution: modify a non-numeric string to a numeric string

two。 Converts a string number of a floating point type to an integer.

Solution: convert to floating point and then to integer

ValueError: substring not found

Description: no substring was found.

Possible reason: when you use the index () or rindex () method to retrieve a string, the specified string does not exist.

Solution: judge before retrieval

ZeroDivisionError: division by zero

Description: 0 cannot be a divisor.

Possible reasons: when carrying out division, integer division and remainder operation, 0 is used as the divisor.

Solution: change to a non-zero number

This is the end of the article on "what are the common mistakes in python and how to solve them?" Thank you for reading! I believe you all have a certain understanding of the knowledge of "what are the common mistakes in python and how to solve them". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report