Do you often encounter errors and exceptions in python

Many of us take the normal route when learning code, including the variables, methods, processes, and logic I use are all designed in advance.

But at work, when we are faced with a new problem or requirement, when we design the code, it is basically impossible to prepare all the data and methods to be used. In many cases, it may be used again and again by running the code. debugging.

During the debugging process, we will encounter such or such problems. These are some errors and exceptions that we cannot avoid when doing code design, including logic errors of business processes, which we need to pay attention to when coding.

So let me introduce you to the errors and exceptions in python

NameError

When an error such as an error is reported when the code is executed, we need to understand what it means.

NameError means that we have accessed an uninitialized variable, for example:

Execute the following code in pycharm:

print(foo)

查看执行结果:

Traceback (most recent call last):

  File "E:/work/test.py", line 1, in <module>

    foo

NameError: name 'foo' is not defined

Among them, NameError: name 'foo' is not defined is the information given to the error of this code. The literal translation of NameError is the name error. Looking at: name 'foo' is not defined means that the name "foo" is not defined. It means that the parameter foo used by the print method is not defined.

If this foo is enclosed in quotes, then it is what we said is a string, a value.

But no, so here, this foo is a variable. Then the variable has no value, and there will be such a problem when it is applied.

The solution is to define:

ZeroDivisionError: division by zero

This error message often appears in the calculation module and involves calculation logic, such as

picture

In the code above, the variable num stores the result of 1/0, but the number 0 cannot be used as the denominator in the calculation logic, that is, it can be used as the dividend. This error is combined with the calculation business, when both numbers need to be filled in , will easily occur.

SyntaxError: Python interpreter syntax error

The SyntaxError exception is the only exception that does not occur at runtime. It indicates that there is an incorrect structure in the Python code, especially the syntax structure, see the following example:

picture

As a person with coding experience, this error will generally not occur, because how to use this syntax is common sense for programmers, but for beginners, we need to pay attention. In the process of designing code, we often forget or consciously get used to it. Hit enter and so on.

The picture above is a typical grammar problem.

Of course, a structural problem that needs to be paid attention to in pyhton is line break or indentation. Python syntax is a language that is very particular about indentation. So when designing code, you need to pay attention. Of course, this error is also very easy to identify:

IndentationError: indentation error

As a coder, we need to pay special attention to indentation, especially when we directly copy a piece of code and use it, we need to pay attention to adjusting the format. For conditional judgment in python, loops, method functions, and classes will be used when using To indentation. Nested loops or nested judgments are also high-frequency scenarios for indentation. For example, the following code:

The execution result is an indentation error. Just adjust the indentation.

Finally: The complete software testing video tutorial below has been organized and uploaded, and friends who need it can get it by themselves [Guaranteed 100% free]

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

Guess you like

Origin blog.csdn.net/wx17343624830/article/details/131904605