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

How to deal with common error reporting problems in Python

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

Share

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

This article mainly introduces how to deal with common error reports in Python. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.

1. The first is common grammatical errors.

two。 Then there are common exceptions in the basic syntax.

3. The last part is the common exceptions in the process of operating the file, which is also important and difficult knowledge.

This is a common error message on the terminal:

According to the definition of the Python official documentation, the "error messages" we see on the terminal can be divided into at least two categories: syntax errors (syntax errors) and exceptions (exceptions).

Syntax errors (syntax errors) are the most common mistakes made by beginners. To put it simply, the code does not conform to the basic syntax specification of Python, which leads to problems with the program.

When your code fully conforms to the syntax specification of Python, it's time to try to run the program. However, in the process of running the program, there may still be problems, which we call exceptions.

For example, two common syntax errors, SyntaxError and IndentationError, as well as their causes and corresponding solutions.

First, let's take a look at SyntaxError.

The first is SyntaxError: invalid syntax (invalid syntax), which is the most common syntax error and is usually caused by the following situations:

First, punctuation is omitted.

Second, misspelling or omission of keywords.

Third, the keyword is used in the variable name or function name.

The second kind: SyntaxError: invalid character in identifier (invalid characters in the identifier), this error is usually due to the use of a large number of Chinese symbols in the code, such as Chinese parentheses, Chinese commas, Chinese colons and Chinese quotation marks. This may be caused by frequently switching between Chinese and English input methods while writing code.

The third: SyntaxError: EOL while scanning string literal (an incomplete string is checked), which is usually due to missing quotation marks on both sides of the string or mixing quotation marks (that is, quotation marks on both sides of the string are not uniform). The solution is also simple: carefully check for omissions in quotation marks at both ends of the string, and remember that single quotation marks match, double quotation marks match, triple quotation marks match.

A summary of the picture:

The second common syntax error to be introduced is IndentationError, or indentation error. In Python, the indentation below the colon helps the program distinguish between layers of code that represent the logic and order in which the code runs. But if the correct indentation level or even indentation is not used under the statement that needs to be indented, the program will report an error.

For example, two common indentation errors:

The first: IndentationError: expected an indented block (code block that needs to be indented), which is mostly due to the fact that there is no indentation in some places that need to be indented, and the way to fix this indentation error is also very simple-- the right remedy, sort out the order and logic of the code, and add indentation where it should be indented (for example: if conditional judgment, for loop, while loop, def function definition, class class creation).

The second kind: IndentationError: unindent does not match any outer indentation level (indentation content does not match any level), this is mainly because the indentation of one line is not aligned with other positions so that the program can not find a matching code block, the solution requires us to sort out the level of the code block and align the indentation.

The summary is as follows:

Then let's take a look at the common exceptions in the basic syntax. The first one to introduce is the common exception TypeError in the basic syntax, that is, type errors.

The way to distinguish between exceptions and errors requires us to observe the terminal prompt. In general, abnormal "error messages" will display Traceback (most recent call last), but syntax errors will not be displayed.

Here are three common types of errors:

First: TypeError: unsupported operand type (s) for... (unsupported operations)

Second: TypeError: can only concatenate str (not "int") to str (string concatenation only)

Third: TypeError: 'xxx' object is not iterable (objects cannot be iterated)

The specific solution can be combined with the following figure:

Bottom line: to avoid type errors, you need to determine what data type you are using (with the help of type ()) and what you need to do to confirm whether you need to use a function for type conversion.

The second exception is IndexError, which is related to the value of the list. The most frequent index error is IndexError: list index out of range (the index is out of range). When using index values in a list, make sure that the index is within the correct range.

Finally, we talked about several common exceptions in the process of manipulating files. The first is that ModuleNotFoundError did not find a module error, which is a module exception. The solution to this exception is very simple, as long as you carefully check whether the spelling of the module is correct when you import the module.

The second is AttributeError attribute error, in order to avoid this exception, when we use the method in the module, we need to check whether the spelling is correct, and then check whether the method or attribute exists in the module or library.

The third is the error that cannot be found in the FileNotFoundError file. In order to avoid this exception, we need to find the corresponding file or folder according to the path and check whether the file or folder name is correct.

The last one is UnicodeDecodeError, which is an encoding and decoding error, which is also an exception thrown by encoding, another parameter of the open () function. The previous course was written as encoding = 'utf-8', because the file encoding format for most work scenarios is' utf-8'. But if you need to use code shared by others, you are likely to throw an exception because of the different encoding formats used between you.

When you encounter such an exception later, there are two solutions:

First, first use software such as VSCode to check the encoding format of the file, and then modify the encoding format of the file to the encoding format you write in the code.

Second, first check the encoding format of the file in VSCode, and then change the encoding parameter in the code to the encoding format of the file.

The summary is as follows:

In fact, we may encounter more than these mistakes in the process of writing code, and we can't cover all of them in the course, so what we need to cultivate is the ability to find and solve errors by ourselves. this requires us to keep trying and learn to search and consult materials on Baidu to accumulate experience.

The above is all the contents of this article entitled "how to deal with common error problems in Python". Thank you for reading! Hope to share the content to help you, more related knowledge, 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