Prompt: Debug Python Code

A copy-ready debugging prompt that gets the model to diagnose before it fixes — with reproduction steps, root cause and a tested correction.

Updated Sep 2026 PIS score 76
The prompt
Open in Studio
You are a senior Python developer. Debug the code below.

Follow this process strictly:
1. State what the code is trying to do (one sentence)
2. Identify the exact line(s) causing the problem and explain why
3. Explain the root cause, not just the symptom
4. Provide the corrected code in full
5. List anything else in the code that will cause problems later

Rules:
- Do not rewrite working parts unnecessarily
- Keep the original style and naming
- If the error message or expected behaviour is missing, ask before guessing

Before answering, verify your proposed fix actually resolves the stated error and does not introduce new ones.

Error message (if any): {{ERROR}}
Expected behaviour: {{WHAT SHOULD HAPPEN}}

Code:
```python
{{PASTE CODE HERE}}
```

Example

Input

ERROR: TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'
WHAT SHOULD HAPPEN: The function should return the running total after adding each item's price.
PASTE CODE HERE:
  def total_price(items):
      total = 0
      for item in items:
          total += item.get("price")
      return total

Output

1. This function is meant to sum the `price` field across a list of item dictionaries and return the running total.
2. Line 4 (`total += item.get("price")`) causes the error: `.get("price")` returns `None` for any item missing that key, and adding `None` to an int raises the TypeError.
3. Root cause: the code assumes every item has a "price" key, but `.get()` silently returns `None` instead of raising a clear error, so the failure surfaces on the addition rather than at the actual bad data.
4. Corrected code:
```python
def total_price(items):
    total = 0
    for item in items:
Show more
        price = item.get("price", 0)
        total += price
    return total
```
5. Other issues: the default of 0 only applies when the "price" key is missing — an item stored as `{"price": None}` still raises the same TypeError, so use `item.get("price") or 0` if None values can occur. Separately, if `price` can arrive as a numeric string like `"9.99"`, adding it to an int raises a new TypeError; convert with `float()` or validate the input if it isn't guaranteed numeric.

Why it works

  • The numbered process forces diagnose-before-fix, stopping the model jumping straight to a rewrite that moves the bug rather than fixing it.
  • Rules against unnecessary rewrites keep the fix minimal and reviewable instead of a wholesale rewrite of working code.
  • The verification step makes the model confirm its own fix resolves the stated error before it is returned.

Why the basic version fails

“Fix my code” usually gets you a rewritten file with the bug moved rather than found. The model jumps straight to producing code because nothing in the prompt asks it to diagnose first.

What this prompt fixes

Forcing the diagnose-then-fix sequence is the single biggest improvement for debugging prompts. Steps 1–3 make the model commit to an explanation before it touches the code, which catches wrong assumptions early. Rule 3 stops the common failure where the “fix” is a full rewrite you didn’t ask for.

Tips per model

  • Claude tends to follow the numbered process in order without needing reminders, and is good at spotting a second latent bug in step 5.
  • ChatGPT occasionally jumps to the corrected code before finishing the explanation; if that happens, ask it to redo steps 1–3 first.
  • Always paste the actual error message and traceback rather than describing it from memory — the exact wording often points straight at the failing line.