AI-assisted coding without losing the plot
A beginner's routine for coding with an AI assistant: small changes, a test as the referee, and a review before you keep anything. Worked through on a real bug, with the real error output.
- Level
- Beginner
- Reading time
- 15 minutes
- Sources checked
- 11 Nov 2026
- Status
- Draft, awaiting editorial review
Who it’s forPeople starting to write small scripts with AI help, and people who use an AI coding tool but don't always understand what it changed.
Before you startPython 3 installed, or willingness to install it. You don't need to be able to write code from scratch.
By the end you'll be able to
- Read the last line of a Python error and say what went wrong
- Use a test to decide whether an AI's change actually works
- Review a change before keeping it, and keep secrets out of your prompts
Why you need a routine
An AI assistant can write code faster than you can read it. That's the risk. If you accept changes you don't understand, you end up with a program that works until it doesn't, and no idea where to start fixing it.
The fix isn't to become an expert first. It's a routine that keeps each change small enough to check, and lets a test, not the assistant's confidence, decide whether it worked.
- 11. One small change at a timeAsk for one thing: fix this bug, add this option. Big requests produce big changes you can't review.
- 22. Give it the evidencePaste the code involved and the exact error. Don't describe the error from memory.
- 33. Let a test be the refereeWrite or keep a test that fails before the change and passes after it. The assistant saying "this should work" isn't evidence.
- 44. Read the change before you keep itLook at exactly what changed. If you can't explain it in a sentence, ask, or don't keep it.
- 55. Save a working versionKeep a copy (or a commit, if you use Git) each time the tests pass, so you can always go back.
Reading an error message
Python's own tutorial gives the two things to know. "The last line of the error message indicates what happened." And "the preceding part of the error message shows the context where the exception occurred, in the form of a stack traceback": which file and line the problem passed through.
So read from the bottom up. The last line tells you the kind of problem. The lines above it point to where. That's usually enough to know which part of your code to show the assistant.
Worked example: a real bug, a real fix
Here's a small function that averages star ratings, with a mistake we put in on purpose. It works for a normal list but fails when there are no ratings yet, which is exactly what happens with a new product.
def average_rating(ratings):
"""Return the mean of a list of 1-5 star ratings, rounded to one decimal place."""
return round(sum(ratings) / len(ratings), 1)Python's unittest module is built in. In its words, "A test case is the individual unit of testing. It checks for a specific response to a particular set of inputs." We wrote two: a normal list, and an empty one that should give no average at all.
import unittest
from ratings import average_rating
class AverageRatingTest(unittest.TestCase):
def test_typical_ratings(self):
self.assertEqual(average_rating([5, 4, 4]), 4.3)
def test_no_ratings_yet(self):
self.assertIsNone(average_rating([]))
if __name__ == "__main__":
unittest.main()E.
======================================================================
ERROR: test_no_ratings_yet (test_ratings.AverageRatingTest.test_no_ratings_yet)
----------------------------------------------------------------------
Traceback (most recent call last):
File ".../test_ratings.py", line 10, in test_no_ratings_yet
self.assertIsNone(average_rating([]))
~~~~~~~~~~~~~~^^^^
File ".../ratings.py", line 3, in average_rating
return round(sum(ratings) / len(ratings), 1)
~~~~~~~~~~~~~^~~~~~~~~~~~~~
ZeroDivisionError: division by zero
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (errors=1)Knowledge check
Read the error
Need a hint?
The last line says what happened. The last file listed above it says where.
Before and after
Asking for the fix
My ratings code is broken, can you fix it?
The test test_no_ratings_yet fails with ZeroDivisionError. Here is ratings.py: [the file]. Here is the failing test output: ZeroDivisionError: division by zero (in average_rating, called with []). Fix average_rating so an empty list returns None. Change nothing else. Reply with only the full corrected file in one Python code block, then one sentence explaining the fix.
def average_rating(ratings):
"""Return the mean of a list of 1-5 star ratings, rounded to one decimal place."""
if not ratings:
return None
return round(sum(ratings) / len(ratings), 1)Its explanation: "The function now returns None for an empty list before attempting the division, so no ZeroDivisionError is raised." Two lines were added and nothing else changed, which is easy to review. Then the referee: we ran the tests again.
test_no_ratings_yet (test_ratings.AverageRatingTest.test_no_ratings_yet) ... ok
test_typical_ratings (test_ratings.AverageRatingTest.test_typical_ratings) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OKKeep your environment and secrets safe
Two habits prevent most beginner trouble. First, use a virtual environment for each project: Python's venv module "supports creating lightweight “virtual environments”, each with their own independent set of Python packages", so an assistant's suggested install can't break your other projects.
Second, never paste a secret into a prompt or a file you share: API keys, passwords, tokens. Leaked keys get misused; GitHub runs secret scanning precisely to catch "exposed credentials before they can be exploited". Keep secrets in environment variables or a file your tools are told to ignore, and if you ever paste one by mistake, cancel it and make a new one.
Sort it
Safe to paste?
Would you paste each of these into an AI coding assistant?
Try it
Guided practice
Your first test-refereed fix
About 30 minutes0 of 6 steps ticked
You’re done when
- Both original tests pass
- You can explain the fix in one sentence
- You've added and passed one test of your own
What you’ll get from it: The routine done once by hand, which is what makes it automatic later.
Free worksheet
The code change review
A one-page check for every change an AI assistant suggests: the evidence, the test, the change itself and anything private.
The online version saves your answers only in this browser. The PDF is A4 and prints on US Letter too.
Your turn
Apply it to your own code
Written answers aren’t marked automatically and aren’t saved: what you type stays on this page until you leave it. Compare yours with our example and the checklist instead.
What to remember
- Small changes, real evidence, a test as referee, and a review before you keep anything.
- Our example is one run on one model with a deliberately simple bug. Real bugs are messier, which is why the routine matters more, not less.
- Coding tools that run commands for you are agents. Read what they're about to do before you approve it.
Tools for this guide
- Aider: AI pair programming in the terminal. It edits files in your Git repository and commits each change, so every edit is easy to review or undo. (Installed only)
- Codex CLI: OpenAI's open-source coding agent that runs in your terminal and can read, edit and run code in your project. (Installed only)
Your progress
You’ve finished 0 of 5 activities in this lesson.
When you've tried the practice task on your own work, mark the lesson complete. It's your call: finishing the activities shows you've practised, not that you've mastered the skill.
Progress is saved only in this browser, on this device. There’s no account, and nothing is sent to us or anyone else. Clearing your browser data, or using another browser or device, starts you afresh. We save which activities you finished, never what you type.
Suggested next lesson
Agents, MCP servers and skills: what each is and when to use oneCoding assistants are often agents: they run commands and change files. Next, what agents, MCP and skills are, and how to keep control.Intermediate, 14 minutesSources and evidence
The routine is our editorial advice. The worked example is real: we ran the test, asked Claude (claude-sonnet-5-5, through the Claude Code command line) for the fix, and ran the test again on 12 November 2026. The files and full output are kept with our research notes. One run, one model: it shows the routine, not how reliable any assistant is.
- Python docs: unittest (opens in a new tab)Checked 11 Nov 2026
- Python tutorial: Errors and Exceptions (opens in a new tab)Checked 11 Nov 2026
- Python docs: venv (opens in a new tab)Checked 11 Nov 2026
- GitHub Docs: About secret scanning (opens in a new tab)Checked 11 Nov 2026
This guide is a draft and hasn’t been through editorial review yet. How we review guides