Who wrote the code and didn't encounter a bug?
To be honest, the most crazy thing about writing code is not that the logic cannot be understood, but that the code runs out - it explodes directly.
"Huh? Why is something wrong?"
You stared at the red error message on the screen and thought to yourself:"No, my code is obviouslyCtrl+V
Come here! ”But it reported an error.
What should I do at this time?
A. Desperately Ctrl+C Close the terminal and go to the fish B. Crazy print() in the code, debug like a primitive person
C. Calm down and be like a real programmer.Using scientific methods Debug
If you choose C, congratulations! This article is for you. Today, I want to teach you howQuickly find bugs and rub them on the ground。
Step 1: Don't panic, the error message is a friend
Have you ever encountered such a situation?
def divide(a, b): return a / b print(divide(10, 0))
Python exploded:
Traceback (most recent call last): File "h:\huajie_python\", line 4, in <module> print(divide(10, 0)) File "h:\huajie_python\", line 2, in divide return a / b ZeroDivisionError: division by zero
Many people see this and go straight to the code and make random changes.wrong! Don't move first!
The error message is actually a clue given to you by Python kindness, which clearly tells you:
- ✅ Error Type:
ZeroDivisionError
(Excerpt from zero error) - ✅ Where to take place: The fourth line of code
- ✅ Specific questions: Try to use 0 to make the divisor
Then how to repair it?It's very simple, check whether b is 0 first:
def divide(a, b): if b == 0: raise ValueError("Can't divide by 0, brother!") return a / b
in conclusion: Before every error,Read the error message 3 times first. 80% of the bugs have actually told you the answer!
Step 2: Stop messing with print, try breakpoint()
Still using print() to debug? Don't do this, we are Python programmers in the 21st century!
print("It's here!") print("x =", x) print("Has the code been executed?")
Doing soThe code is full of debugging garbage, which makes it a headache to delete!Python actually has a more elegant method built in:breakpoint()
。
How to use breakpoint()?
def calculate(x): result = x * 2 breakpoint() # Pause here return result print(calculate(5))
After running, the program willPause at breakpoint(), you can enter the following command:
x
→ Check what the value of x isresult
→ Check if the calculation is correctc
→ Continue to execute
This is 100 times more efficient than print()!
Step 3: Dichotomy Debug, quick locking of bugs
If the code hasHundreds of lines, Where is the bug hidden? You won't reallyCheck it out one by one?
Smart people use the "dual method" to debug:
- 1️⃣ Comment out half of the code first, see if the bug is still there.
- 2️⃣ If the bug is still there, it means it is in the rest of the parts.
- 3️⃣ If the bug disappears, means it is in the commented code.
- 4️⃣ Continuously narrowing the scope, until the bug is found accurately.
For example:
def process_data(data): step1 = () step2 = int(step1) # 💥 There may be an error here step3 = step2 * 10 return step3
Don't know where the mistake was wrong?
Comment out part of it first:
def process_data(data): step1 = () # step2 = int(step1) # step3 = step2 * 10 return step1
Run it and see: If that's right, it means that the bug is in the commented part; if it still reports an error, the problem isstep1
。
Do this and you can lock in bugs in the fastest way!
Step 4: If you don’t know how to ask DeepSeek, but you have to ask well!
Asking questions is also very important!Most people askDeepSeekThe way is:
- ❌
"Python error"
(Too general) - ❌
"Python is not working"
(Isn't this nonsense)
The correct way to ask questions is specific:
- ✅
"TypeError: 'int' object is not iterable in Python"
- ✅
"Python does not work"
The more accurate the keywords are, the more accurate the AI can solve them!
In addition, if AI cannot solve it, you can try it if you have the conditionsStack OverflowSearch, basically 99% of Python problems have helped you get stuck.
Step 5: Find a great master to help you (the fastest way)
It is the most effective and fastest way to find someone to help with some problems, but it is necessary to describe the problem appropriately.
Some people seek help as follows:
- ❌
"Help, my code is wrong"
- ❌
"What does Python mean?"
No one wants to help you!
✅ The correct way to ask questions:
Describe the problem:I'm encountering TypeError while sorting the list
Provide code:
my_list = [3, "hello", 5] my_list.sort()
Give an error message:
TypeError: '<' not supported between instances of 'str' and 'int'
The solution you've tried:
✔️ Remove"hello"
Try it, can run
✔️ Usesorted()
Not OK
In this way, others will understand the problem you are facing at a glance and are willing to help you!
Be an efficient Debug player!
Debugging Python code,The key is the right method:
- ✅ Understand the error message, don’t panic!
- ✅ Use breakpoint() and don't print() to debug!
- ✅ Dichotomous method to check bugs and quickly lock the problem!
- ✅ Ask DeepSeek in the AI era to ask questions accurately and avoid detours!
- ✅ Ask questions clearly, only the great god is willing to help you!
Master these skills and you can debug 10 times faster!
The above is the detailed content shared by efficient methods of Python debugging code. For more information about Python debugging code, please pay attention to my other related articles!