Checking AI-Written Python Code: A Six-Step Habit That Finds Three Hidden Faults
AI code is a fast first draft, and you sign it. One cart-total script with three planted faults: a method that does not exist, a crash on an empty cart, and > where the requirement meant >=. Each is found by a different step.
What you will learn
- Apply the six-step habit: read, run, edge cases, requirements, ask why, commit
- Find a method that does not exist by running the code
- Catch a crash with an edge case like an empty cart
- Spot an off-by-one comparison by checking output against the requirements
Before you start
- Python Conditionals and Loops
- Run a Python File and Read the Error
- Prompting Fundamentals for Code
Think about a friend who writes a letter for you and finishes it in a minute. It's neat, it reads well, and most of it is right. That's a big help. But you read it before you sign, and you spot a wrong date, because you know what it should say. You fix that, and only then does your name go at the bottom.
Code from an AI tool works the same way. It isn't good and it isn't bad. It's a fast first draft, and you're the one who signs it.
The habit: six steps, in order
- Read it, and say each line in your own words.
- Run it, and see what it does.
- Try edge cases, the odd inputs nobody mentioned.
- Check it against the requirements, what you asked for.
- Ask why about anything you can't explain.
- Only then, commit.
A small job
Here's what we want: add up the prices in a cart, give ten percent off when the total is 100 or more, and print the total and the average price. Here's an example of code an AI tool might return. It's short and looks tidy, but three faults are hiding in it.
# An example of code an AI tool might return
cart = [40.0, 35.5, 24.5]
total = 0
for price in cart:
total = total + price
average = total / len(cart)
if total > 100:
total = total * 0.9
total = total.round(2)
print(f"Total: {total:.2f}")
print(f"Average: {average:.2f}")
Step 1: read it
Don't run anything yet. The cart has three prices. The loop takes the total to 40, then 75.5, then 100. The average is the total divided by how many prices there are. If the total is more than 100, we keep 90 percent of it.
Then there's total.round(2), and you can't explain that one, because you haven't seen it anywhere. Don't guess. Put a question mark beside it. If you can't say what a line does, that line isn't checked yet.
Step 2: run it
Traceback (most recent call last):
File "S01L09_1_CheckingAICode\ai_draft.py", line 13, in <module>
total = total.round(2)
^^^^^^^^^^^
AttributeError: 'float' object has no attribute 'round'
Last line first: a float has no round method, and it points at line 13, our question mark. The code calls a method that doesn't exist. It looks right, and that's exactly why you run it. That's fault one. You don't need the line at all, because :.2f in the print already shows two decimals, so delete it.
Total: 100.00
Average: 33.33
No traceback now, so it's tempting to stop and commit. But a program that runs can still be wrong. All you know is that it finished, for this one cart.
Step 3: edge cases
An edge case is an input at the limit, the kind that's easy to forget. What if the cart is empty? What if there's only one price? What if the total lands exactly on a limit? Look at your inputs and ask: what's the smallest, the emptiest, and what sits right on the line?
Change only the first line to cart = []:
Traceback (most recent call last):
File "S01L09_1_CheckingAICode\empty_cart.py", line 7, in <module>
average = total / len(cart)
~~~~~~^~~~~~~~~~~
ZeroDivisionError: division by zero
The loop adds nothing, the length is 0, so it's 0 divided by 0. It's the same crash as the average() example in Thinking like a Python architect. That's fault two. The requirements didn't mention an empty cart, so the behaviour is your decision. Here, we'll print "Cart is empty".
Step 4: check the requirements
Put what you asked for beside what you got. Adding up: 40 + 35.5 + 24.5 is 100, right. Total and average printed: both there. Ten percent off at 100 or more: the total is exactly 100, so it should print 90.00. It printed 100.00. No error, just a wrong answer.
Why? The code says total > 100, and 100 isn't greater than 100. You even read past it in step one. Change it to >= and 100 moves inside. Fault three: one small character, and the requirements showed it.
Step 5: ask why
Pick a line you'd like explained and ask the tool, for example: "Why is the average worked out before the discount?" The order matters. The average line sits above the if, so it uses the full prices. You asked for the average price of the items, so that's what you want; if you'd wanted something else, this is where you'd catch it.
A clear answer still isn't proof, so check what the tool tells you against the code. If you can't follow it, ask again in simpler words. You should be able to explain every line yourself before you keep it.
The fixed version
carts = [
[30.0, 20.0],
[40.0, 35.5, 24.5],
[],
]
for cart in carts:
if len(cart) == 0:
print("Cart is empty")
continue
total = 0
for price in cart:
total = total + price
average = total / len(cart)
if total >= 100:
total = total * 0.9
print(f"Total: {total:.2f}")
print(f"Average: {average:.2f}")
Total: 50.00
Average: 25.00
Total: 90.00
Average: 33.33
Cart is empty
Three carts, so one run covers every case. 50 is under the limit, so no discount. The second cart reaches 100 and now gets it: 90.00. The empty cart prints a message instead of crashing. Three faults, each found by a different step: the run, the edge case and the requirements.
Step 6: commit
Every check has passed, so run down the list once more, then save this version with Git and a short message that says what it does (see Git and GitHub fundamentals). If something breaks later, you can come back to this point. And remember the letter: your name is on this now, whoever wrote the first draft.
Keep it as a checklist
You can keep the habit right beside the code as comments:
# Paste the program your AI tool wrote below.
names = ["Asha", "Ben", "Chen"]
for name in names:
print(f"Hello, {name}!")
# 1 Read it: what does each line do?
# Each name gets one greeting line.
# 2 Run it: does it finish without an error?
# Yes, three lines print.
# 3 Edge case I tried: an empty list.
# Nothing prints, and nothing crashes.
# 4 The requirements say: greet every name.
# It does.
# 5 Question I asked the tool: why a for loop?
# One line of code covers any number of names.
# 6 Commit when every check passes.
Hello, Asha!
Hello, Ben!
Hello, Chen!
Quick recap
| Step | Found here |
|---|---|
| Read | a line you couldn't explain: total.round(2) |
| Run | AttributeError: that method doesn't exist |
| Edge cases | ZeroDivisionError on an empty cart |
| Requirements | > where "100 or more" meant >= |
| Ask why | confirms the average uses the full prices |
| Commit | only after all of the above |
Try it yourself
Ask an AI tool for a small program, then find one case it missed.
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
Python for AI: The Five Tools You Actually Need
The small part of Python that carries most AI code: dicts and JSON, typed functions with a dataclass, comprehensions and f-strings, environment variables for keys, and try/except for replies that are not JSON. Three standard-library scripts on four support tickets.
Thinking Like a Python Architect in an AI-First World
Coding is laying bricks; architecting is deciding how the pieces fit. A two-line average function shows why someone still has to understand the code an AI tool writes.
Your First Python Program: print, Comments, sep and end
Five tiny files: print and quotes, top-to-bottom order, # comments, several values with sep and end, and the SyntaxError one missing quote causes. Every output is from a real run.
Set Up PyCharm for Python: Install, First Project and First Run
Install PyCharm, create a Pure Python project, and understand the two words that confuse beginners: the interpreter (the cook) and .venv (the project's own shelf). Then run a file and read the exit code.
