Lesson 2 of 9
Module 3 · Coding with AI assistants
The assistant only knows what you tell it. A good prompt gives the context, the exact input and output, the constraints, and an example you can check.
Why vague prompts give vague code
A prompt is the text you give the assistant. Everything you leave out of it, the assistant fills in with whatever is most typical in its training data. Sometimes the typical choice is what you wanted; often it is not, and you will not be told which choices it made. The comparison below shows how many decisions hide inside one short request.
- “Write a function for GC content.”
- Which language? Percent or fraction? Lower case? Ns? Empty input?
- Language and version, inputs and outputs, edge cases, an example with its expected answer, and what not to use
Take the questions on the left one at a time. "GC content" could be returned as a percentage (66.67) or a fraction (0.67), and code that mixes them up gives answers a hundred times out. Sequences straight from a file may be in lower case, which a naive count of "G" will miss. N is the standard letter for a base the sequencer could not read; should it count towards the length or not? An empty sequence could return 0, return nothing or stop with an error. None of these choices is wrong in itself, but your analysis needs one particular answer to each, and only you know which.
Here is what that tends to look like in practice. Given only the vague request, an assistant will often write something like (seq.count("G") + seq.count("C")) / len(seq). It returns a fraction, misses lower-case bases, counts N in the length and crashes with a ZeroDivisionError on an empty string. Each of those is a typical choice, and none of them is mentioned in the reply.
The same gap appears with figures. "Plot expression by group" leaves open the chart type, whether to show every point, what the error bars mean and which colours to use: exactly the decisions you learned to make on purpose in Comparisons and uncertainty and Colour that works for everyone. An assistant will make them for you, by default, unless you say what you want.
A useful habit when you are not sure what to specify: ask the assistant to list the questions it would need answered before writing the code, and answer them yourself. That turns its hidden assumptions into visible ones before any code exists.
A prompt template
The prompt below asks for three small functions that the rest of this module builds, tests and debugs. Each line does a job.
Context: I am analysing DNA sequences in Python 3.12, standard library only.
Task: write three functions in a module called seqtools.py:
1. gc_content(seq) -> percentage of G and C, case-insensitive
2. reverse_complement(seq) -> reverse complement; N stays N
3. mean_quality(qual) -> mean Phred score of a FASTQ quality string (Phred+33)
Edge cases: an empty sequence should raise ValueError.
Examples: gc_content("GGCCAT") is 66.67; reverse_complement("AAGC") is "GCTT";
mean_quality("IIII") is 40.
Style: short docstrings and type hints. Do not use Biopython.
Then write pytest tests for each function, including the edge cases.What each part does
- Context. Naming Python 3.12 and "standard library only" rules out code that needs a different version or extra packages. The standard library is the set of modules that comes with every Python installation.
- Task. The file name, seqtools.py, and the exact function names mean the code will fit what you import later. Each line says what goes in and, after ->, what comes out. Reverse complement is the sequence of the opposite DNA strand, read in its own direction: complement every base (A↔T, G↔C), then reverse the order. Phred+33 names the encoding of FASTQ quality scores, so there is no guessing about the offset.
- Edge cases. A ValueError is Python's standard error for "the type is right but the value is not acceptable". Asking for it means an empty sequence stops the program with a clear message instead of failing somewhere else later.
- Examples. Three inputs with their correct outputs. GGCCAT has four G or C out of six bases, 4 ÷ 6 × 100 = 66.67%. The reverse complement of AAGC is GCTT. You can check every one of these by hand, and the assistant's code must reproduce them.
- Style. A docstring is a short description written as the first line of a function; a type hint, such as seq: str, records what kind of value a function expects. "Do not use Biopython", a widely used Python library for biological sequences, keeps the code self-contained and short enough for you to read every line.
- Tests. The last line asks for pytest tests, small functions that check the code automatically. You will write and run them in Testing AI-written code.
The template works because it moves decisions from the assistant to you. Every line answers a question the assistant would otherwise answer with a guess. It is not a magic formula; it is the list of things a careful colleague would ask before writing the code.
One detail in the examples is worth noticing now. 4 ÷ 6 × 100 is 66.666…, not exactly 66.67. A test that checks gc_content("GGCCAT") == 66.67 would fail for correct code. In Testing AI-written code you will compare numbers with a small tolerance instead, using pytest.approx. Decimal numbers are stored in binary on a computer, so exact equality checks on them are a common source of false failures.
The same structure adapts to other tasks. For a pandas task, the context might name the pandas version and give the column names and types of your table; the task names the function and what it returns; the edge cases cover missing values and an empty table; the example is three rows with the result you worked out by hand. Two common mistakes are leaving out the column names, so the assistant invents them, and writing rules that contradict each other, such as "keep it short" and "handle every possible input".
Worked example · why the examples matter
stating the expected answer forces the assistant (and you) to handle the +33 offset
and gives you a check you can run in one line
Step by step. Every character a computer stores has a number, its ASCII code; capital I is 73. In Phred+33 encoding the quality score is that code minus 33, so 73 − 33 = 40. The string "IIII" holds four identical characters, so the mean is (40 + 40 + 40 + 40) ÷ 4 = 40. A Phred score of 40 means the sequencer estimates a 1 in 10,000 chance that the base call is wrong.
What the answer means. Code that forgets the offset returns 73 for the same string, and code that uses the old +64 offset returns 9. Both numbers look like plausible quality scores, so neither mistake would be obvious from the output alone. Writing 40 into the prompt turns a silent mistake into a test that visibly fails.
Go deeper: paste your tests into the prompt
Examples written in words can still be read two ways. Examples written as tests cannot. You can paste lines such as assert reverse_complement("ATGN") == "NCAT" into the prompt and ask for code that makes them pass. The expected values are still yours, worked out by hand, so the tests stay independent of the code.
Watch for one trap. When a test fails, an assistant, and especially a coding agent that can edit files, will sometimes make it pass the wrong way: by editing the test, or by adding a special case such as if seq == "GGCCAT": return 66.67. The test then passes and proves nothing. Treat your test file as locked. If the assistant changes it, find out why before you accept the change.
Keep the conversation focused
- Ask for one small piece at a time, and test it before asking for the next.
- Paste the relevant code and the exact error, not your whole project.
- Ask it to explain its choices and list its assumptions. Assumptions are where the bugs hide.
- If an answer goes in circles, start a new conversation with a clearer prompt.
- Say what you have already tried, so the assistant does not suggest it again.
The reason for working in small pieces is the same as in any careful lab work: if you change one thing at a time, you know which change caused a result. A request for a whole pipeline at once gives you hundreds of lines in which a single wrong assumption is hard to find. A request for one function gives you twenty lines and an example to test them against.
Starting a fresh conversation helps because the assistant takes the whole conversation so far as its context. After several wrong attempts, those wrong attempts are part of what it is predicting from. A new conversation with a better prompt removes them.
Asking for assumptions is most useful when you check the list against your own. A reply to the prompt above might list, for example, that N counts towards the length in gc_content, or that lower-case input comes back in upper case from reverse_complement. Each assumption is either what you wanted, something to add to the prompt, or a test to write. An assumption you disagree with is a bug found before you have run anything.
Two practical limits apply. Paste only what is safe to share: never patient data, passwords or access keys, a topic for Privacy, security and licences later in this module. And know when to stop. If three attempts at a small function have failed, writing it yourself, with the assistant explaining any step you are unsure of, is often faster and teaches you more.
The advice to paste only the relevant code has some research behind it. Liu et al. (2024), “Lost in the middle: how language models use long contexts”, Transactions of the Association for Computational Linguistics, found that models used information placed at the start or end of a long input more reliably than information buried in the middle.
Treat this as a reason for focus, not a fixed rule. The study tested question answering and retrieval tasks on models from 2023, and models with longer context windows have improved since. The context window is the amount of text a model can take in at once, measured in tokens. The practical lesson still holds: a short, focused prompt leaves less room for the important line to be overlooked.
Key point
Work out at least one expected answer by hand before you ask. If you cannot say what the right output is, you cannot tell whether the code is right.
A second example. Before asking for a function that counts reads per gene, count them yourself for a tiny made-up input: three reads and two genes, where you know that gene A should get two reads and gene B one. That small example takes a minute to write and catches mistakes such as counting a read twice or dropping reads at the ends of genes.
Further reading · Module 3Wilson et al. 2014: testing and review habits that apply to AI-written code too. · All reading for this course
Check your understanding
1. What most improves a code prompt?
2. Why ask the assistant to list its assumptions?
3. What is the mean Phred score of the quality string “5555” (“5” is ASCII 53)?
Up next
Lesson 3
Read before you run