SoFunction
Updated on 2025-05-22

Use and instructions for debugging functions in pycharm

1. Preparation before debugging​​

1. Prepare a test code

First write a simple Python script (such as calculating factorials) and deliberately leave some questions:

def factorial(n):
    result = 1
    for i in range(n):
        result *= i
    return result

print(factorial(5))  # Expected output120,But the actual output0?

2. Why is debugging required?

  • When the code runs the result not in the expectations, debugging can help you track the code execution process line by line.
  • You can view the changes in the variable value and find out logical errors.

​​2. Debugging core operations (with screenshot steps)​​

​​1. Setting Breakpoint

  • Function​: Let the program be paused when it runs here for easy observation.
  • ​Operation​: Click on the left side of the code line number, and a red dot appears.
  • (Example: on line 3result = 1and line 5result *= iSet breakpoints)

2. Start debug mode

  • Method 1​: Click the green beetle icon in the upper right corner 🐞.
  • Method 2​: Right-click code → SelectDebug 'file name'

3. Debug interface layout

After starting debugging, PyCharm will automatically enter the debug view, which is mainly divided into:

  • ​① Debug toolbar​: Control code execution (step, recovery, etc.).
  • ​② Variables​: Displays the variable value in the current scope.
  • ​③Console​: Display program output and input.

​​3. Detailed explanation of key debug buttons

Button Icon name shortcut key Description of function
▶️ Resume F9 Continue to execute the program until the next breakpoint or end.
➡️ Step Over F8 Execute the current line of code without entering the function or loop inside.
⬇️ Step Into F7 Execute the current line of code and enter the function inside (such as when debugging custom functions).
⬆️ Step Out Shift+F8 Jump out from inside the current function and return to where it was called.
🛑 Stop Ctrl+F2 Terminate the debugging process.

​​4. Actual debugging process (taking the example code as an example)​​

​​1. First pause (breakpoint: line 3)​​

  • Observe the variable window​: At this timen=5result=1(correct).
  • Click Step Over(F8)​​: Executeresult = 1

​​2. Enter the loop (breakpoint: line 5)​​

ClickStep Over(F8) Multiple times​, observe each cycleiandresultChanges:

  • ​Problem exposed​: During the first cyclei=0,lead toresult *= 0, the result becomes 0!
  • Error Cause​:range(n)What is generated is[0,1,2,3,4], should1Start the loop.

3. Repair the code

Modify the loop range:for i in range(1, n+1):

5. Advanced debugging skills

1. Conditional Breakpoint

  • Function​: Pause when a specific condition is met.
  • Settings​: Right-click the breakpoint → Enter the condition (such asi == 3)。

2. Monitoring expressions (Watches)

  • Function​: Monitor the value of a variable or expression in real time.
  • ​Operation​: AtWatchesWindow click+→ Enter expression (egi * 2)。

3. Rerun debugging

  • After modifying the code, you do not need to close debugging, just click the debug button to restart.

​​​6. Troubleshooting of FAQs​​

1. The program is not paused during debugging? ​​

  • Check if a breakpoint is set.
  • Make sure the code is indeed executed to the breakpoint position.

2. The variable window does not display the value? ​​

  • Confirm that the program has been paused at the breakpoint.
  • Check whether the variable is within the current scope (for example, the variables inside the loop need to be viewed in the loop).

3. How to debug an error line? ​​

  • When the program throws an exception, PyCharm will automatically jump to the error line and display the stack information.

Summary: The core idea of ​​debugging

  • ​Locate the problem area​: Reduce scope through error prompts or logs.
  • Set breakpoints​: Pause before and after suspicious code.
  • ​Line-by-line tracking​​: Observe whether the variable meets expectations.
  • ​Verification Fix​: Repeat debugging after modification until correct.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.