In this section, we will learn about program debugging. Some students may have questions. Can’t we see the debugging results when we run the program? But everyone needs to know that the design of the program must be logical and top-down. The design structure is conducive to our understanding of the program, so we ensure that the program is more useful for development from top to bottom, and debugging is the best way to help us complete this design concept. Below we learn two debugging methods, one is to use The debugging function that comes with IDLE is to use the assert statement for debugging.
I don’t know if you have noticed the ‘Debug’ option above when using IDLE.
There is another option called 'Debugger' in the 'Debug' option. When we click it, the interface shown below will appear. The [DEBUG ON] in the middle means to turn on the debugging state.
Adding method: Right-click the statement you want to add, and then select Set Breakpoint in the pop-up menu, that is, the corresponding breakpoint is added. If you want to clear the breakpoint, select Clear Breakpoint in the same way.
The function of a breakpoint is to interrupt the execution of the program when it reaches the breakpoint. We can view information such as variables in the current state.
After adding a breakpoint, it looks like this:
What the button does:
Go: Execute to breakpoint.
Step: Enter the function to be executed
Over: single step execution
Out: jump out of the current function
Quit: end button
When we debug, press F5 on the program interface to start, press Go to start execution to the first breakpoint, and we can observe the value of variable n.
At the first breakpoint, m is 5 and n is 20. Press the Go button again to enter the next breakpoint.
At this time, the value of n changes, and we press Go again to enter the next breakpoint.
The value of n changes again, there is no breakpoint, and it ends after pressing Go.
Debugging is complete.
The program debugging function that comes with IDLE can help us gradually analyze the global variables in the program. We can also see the local variables of the current function through line positioning. When we turn a local variable into a global variable, we can also use the break The test after clicking directly shows that local variables are converted into global variables. This debugging method can also help us understand other people's programs and provide great help for our learning.