Pycharm code debugging - IDE's breakpoint debugging

 

The most commonly used method of Pycharm is to use the debugging function of the IDE, set breakpoints at key codes, and view the values ​​​​of key variables.

What is setting a breakpoint?

Setting a breakpoint is to set certain code line locations, and when the program runs to these locations, execution will be suspended.

When we check the value of a certain variable while the program is running, we must make the running program stop at the corresponding position.

Let's take Pycharm as an example and take a look.

First check whether the key variables that determine the program's running results and the running results of the key codes are consistent with expectations

So we can set a breakpoint at the following code

It is very simple to set a breakpoint in Pycharm. On the left border of the code where the breakpoint is to be set, which is where the arrow in the above picture points, just click the mouse.

After setting, a red dot will appear as shown in the picture above.

Next, on the code editing interface, right-click the mouse and select debug to debug and run the program, as shown in the figure below

注意不要选Run, Run是直接运行,不会停留在断点处

After a while, you will find that the program has run in debug mode and will stay at the breakpoint just set. As follows

Note that the highlighted line indicates that program execution is suspended here, and the highlighted line of code  has not yet been executed

At this point, we want to check the value of the variable content. Since the currently paused line has not been executed, the content variable has not received a value. We need to execute a line of code to complete the execution of the current line before the content variable will be assigned a value.

So how to execute a line of code?

IDE usually has 2 ways.

One is step over, which corresponds to the button circled below

Click this button, and the IDE will let the current program execute the code of the current line. If there is a function call in the code, the execution  will not pause in the function  , but directly run all the code in the function, and pause in the next line of code.



The other is step into, which corresponds to the button circled below

Click this button, and the IDE will let the current program execute a step of the current line of code. If there is a function call in the code, execution  will be suspended in the function  .


Here we can click the Step Over button, and after executing all the current lines, the cursor will stop at the next line, and the variable content value will be displayed in the Variables window below, as shown in the figure

Since the string is relatively long, we can click the view at the arrow, and the following information box will pop up, showing the complete variable content

It can be seen that the read content is correct.

Then the next key variable to be checked is in the back, and the code for judgment.

Therefore, you can set a new breakpoint, as shown in the following figure

Then click the button shown in the figure below to continue debugging the program, and the program will stop at the next breakpoint


At this time, if we observe carefully, the two variables to be compared, mostsoldcount and soldcount

Careful readers will find that the types of these two variables are both strings.

Compare the size of strings? ? ! ! !

This is the problem: we forgot to convert the string to an integer type, which caused problems when comparing sales.

At this point, you can end the debugging. Modify the code and add the operation of converting to an integer, as follows

    soldcount = int(items[-2])

Run it again and find the result as follows

最热卖手机是 华为Mate10, 销量是 170

That's right.

Guess you like

Origin blog.csdn.net/weixin_47649808/article/details/126311180