25  AI: Let it explain, then confirm

The assistant as Explainer: an explanation is useful but provisional.

The move for this whole session: paste something, a traceback or a piece of code, ask the assistant to explain it, read the explanation, and only then run the actual code and read what really happens. An explanation you have not confirmed is a plausible story, not knowledge. Every round below ends the same way: does what actually happened match what the assistant said would happen, in every detail, or only in the parts that were easy to guess?

TipThe move, every time
  1. Paste. Give the assistant the error or the code, with no extra hints about what you already suspect.
  2. Read the explanation. Note the specific claim it makes: which line is at fault, what value caused it, what the code will print.
  3. Confirm. Run the code yourself. Compare what actually happened, line for line, against the specific claim, not just the general vibe of it.

Syntax errors

Each snippet below fails before it ever runs. Read the assistant’s explanation of why, then run the snippet yourself and confirm the error message matches its story exactly, not approximately.

Exercise 25-1

AI: Explainer

def gc_content(dna:
    return dna.count('G') / len(dna)

Exercise 25-2

AI: Explainer

sequences = ['ATG', 'GCC', 'TAA'
print(sequences)

Exercise 25-3

AI: Explainer

for base in 'ATGC':
print(base)

Exercise 25-4

AI: Explainer

name = 'Preben'
print('Hello, ' name)

Runtime errors

These run, and then fail partway through. Read the traceback yourself first, note the line and the error name, before you ask the assistant anything, so you have your own prediction of the cause to compare against its explanation.

Exercise 25-5

AI: Explainer

counts = {'A': 4, 'T': 3, 'G': 2}
print(counts['C'])

Exercise 25-6

AI: Explainer

bases = ['A', 'T', 'G', 'C']
print(bases[4])

Exercise 25-7

AI: Explainer

def average_length(sequences):
    total = sum(len(s) for s in sequences)
    return total / len(sequences)

print(average_length([]))

Exercise 25-8

AI: Explainer

def combine(dna, count):
    return dna * count

print(combine('ATG', '3'))

Explaining code that runs fine

Not every explanation is about a failure. Here the code works; the question is whether the assistant’s account of what it does is actually right, which you confirm by predicting the printed output yourself before running it.

Exercise 25-9

AI: Explainer

dna = 'ATGGCCTAA'
print(dna[::-1])

Exercise 25-10

AI: Explainer

counts = {}
for base in 'ATGGCCA':
    counts[base] = counts.get(base, 0) + 1
print(counts)

Exercise 25-11

AI: Explainer

sequences = ['ATG', 'GCCTAA', 'TT']
longest = max(sequences, key=len)
print(longest)

Catch the wrong explanation

This one is curated so the assistant’s explanation, while fluent and structured exactly like the correct ones above, gets something specific and checkable wrong. Do not skip the confirm step here even if the explanation sounds completely convincing, especially if it sounds completely convincing.

Exercise 25-12

AI: Explainer

def add_base(bases=[]):
    bases.append('A')
    return bases

print(add_base())
print(add_base())

Ask the assistant to explain, line by line, what each call to add_base() prints, and why. Run it. Where exactly does the explanation and the real output part ways, and what does that tell you about default argument values in Python?

Exercise 25-13

AI: Explainer

original = ['A', 'T', 'G']
copy = original
copy.append('C')
print(original)

Ask for an explanation of what print(original) will show, predicting first yourself. Where the assistant’s explanation and the actual output disagree, or where it gets the right output but for a reasoning that would fail on a different example, is exactly the case this exercise exists to surface.

Part 5, Bring your own

Find one error message or one piece of code from your own work this term, ideally from this week’s exercises, that confused you at the time.

Exercise 25-14

SOLO

Before asking the assistant anything, write down, in your own words, what you now believe was actually happening, using what you have learned since.

Exercise 25-15

AI: Explainer

Ask the assistant to explain the same error or code. Compare its explanation to the one you wrote yourself in Exercise 14. Confirm both against the actual running code. Which explanation was more exact, and which, if either, missed a detail that mattered?

Logbook

For your logbook this week, describe the one explanation from today’s session that sounded most convincing and turned out to be wrong, or incomplete, once you confirmed it by running the code. Say specifically what detail the explanation glossed over, and how you would rewrite it so it would have survived confirmation.