Debugging with AI: give it the code, the error and what you expected

Debugging with AI: give it the code, the error and what you expected

AI for software developmentIntermediate2 minবাংলা

You need: ChatGPT (chatgpt.com), Python 3, pytest

Write “my code doesn’t work, fix it” and the AI answers with a guess. Simon Willison, in his post on using LLMs for code, says most of the craft comes down to context: what you show the AI. For debugging you need three things: the code, the full error, and what you expected.

Step 1: Run the bug yourself first

Our online shop writes prices like “৳ 1,250”. This function should read the number out of that text:

price.py
def parse_price(text):
# "৳ 1,250" -> 1250
return int(text.replace("৳", "").strip())
print(parse_price("৳ 1,250"))

Running it gives the real error:

Terminal output
Traceback (most recent call last):
File "price.py", line 5, in <module>
print(parse_price("৳ 1,250"))
File "price.py", line 3, in parse_price
return int(text.replace("৳", "").strip())
ValueError: invalid literal for int() with base 10: '1,250'

Step 2: Give all three together

Paste the whole traceback, not just the last line. Say in what order you want the answer: cause first, then the fix, then tests. And describe what your real data looks like. Our site also shows prices in Bangla digits, so we said so.

Request
My Python function crashes. Explain the cause first, then give the smallest fix, then write pytest tests.
Code (price.py):
def parse_price(text):
# "৳ 1,250" -> 1250
return int(text.replace("৳", "").strip())
print(parse_price("৳ 1,250"))
Error:
Traceback (most recent call last):
File "price.py", line 5, in <module>
print(parse_price("৳ 1,250"))
File "price.py", line 3, in parse_price
return int(text.replace("৳", "").strip())
ValueError: invalid literal for int() with base 10: '1,250'
Expected: 1250. Prices on our site can also look like "৳950", " ৳ 1,250 " or use Bangla digits, like "৳ ১,২৫০".

Tap the picture to see it full size

  1. Code, error and expectation together
  2. Send button

Step 3: Read the cause first

Tap the picture to see it full size

  1. The cause, then the smallest fix

The cause is simple: the ৳ and the outer spaces were removed, but the comma in the middle stayed. The fix removes commas with .replace(",", ""), and this time ChatGPT also converted Bangla digits to ASCII digits with str.maketrans before calling int().

(In our Bangla run of the same test, ChatGPT instead said Python’s int() already reads Bangla digits, so no conversion is needed. Two different answers; we tested both.)

Step 4: Run the AI’s tests on your machine

Tap the picture to see it full size

  1. The AI's tests: five inputs, including Bangla digits

Put the fixed function in price.py and the tests in test_price.py, then run them. The real result on our machine:

python -m pytest -q
..... [100%]
5 passed in 0.01s

All five pass, including “৳ ১,২৫০”. The other answer, without the conversion, also passed its own four tests: Python’s int() really does accept Bangla digits. We know that now because we ran it.

Step 5: Think beyond the tests

Tests only prove what you asked. We ran two more inputs that weren’t in the request:

New inputs, same function
'Tk 1,250' -> ValueError: invalid literal for int() with base 10: 'Tk 1250'
'৳ 1,250.50' -> ValueError: invalid literal for int() with base 10: '1250.50'

Both crash. That isn’t the AI’s fault; we never mentioned them. Look at how prices really appear in your database, and add those forms to the request and the tests.

Sources

Screenshots and steps checked: . AI answers can be wrong. For anything that matters, such as money, health, law or exams, always check against the original source or an expert. Never give any AI your password, PIN, national ID or bank details.

Next post
AI and cyber securitySecurity-checking code with AI: catching SQL injection