ChatGPT Coding

Debug a Python Script

Prompt
I have a Python script that [describe issue]. Here's the code: [paste code]. Walk me through what's wrong, provide the corrected code, and explain why the bug happened so I can avoid it in the future.
Why it works

Teaching while debugging prevents repeat mistakes instead of just handing over a fix.

If you're struggling with a Python script that's not working the way you expected, the Debug a Python Script prompt is designed to help you understand exactly what went wrong and how to fix it yourself. This prompt is ideal for developers of all skill levels who use ChatGPT for coding help. Whether you're a beginner learning Python fundamentals or an experienced programmer debugging complex logic, this approach turns ChatGPT into a teaching tool rather than just a code-fixing service. Instead of blindly accepting a corrected version, you'll learn the reasoning behind the bug, which helps prevent similar mistakes in future projects.

Using this prompt is straightforward. Start by clearly describing the issue you're encountering in the first placeholder. For example, you might write "my function returns the wrong output when I pass a list with duplicate values" or "my loop is running infinitely and I can't figure out why." Then paste your actual code into the second placeholder. A complete example would look like: "I have a Python script that's supposed to sort a list but it's throwing a TypeError. Here's the code: [your code here]. Walk me through what's wrong, provide the corrected code, and explain why the bug happened so I can avoid it in the future."

When you use this prompt effectively, ChatGPT will provide a detailed explanation of what's causing the problem, show you corrected code with comments, and walk through the specific concept or Python feature you misunderstood. This three-part response ensures you don't just fix this bug but develop better debugging instincts.

One pro tip for better results is to include any error messages or unexpected outputs you're seeing. If your script throws an exception, paste the full traceback. If it runs but produces wrong results, show what you expected versus what you got. This context helps ChatGPT pinpoint the issue faster and explain it more accurately.