24  AI: Let it explain, then confirm

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

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. Does what actually happened match what the AI claims in every detail?

  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 even starts running. Read the assistant’s explanation of why, then run the snippet yourself in a new code block and confirm that the error message matches its story exactly, not approximately.

Exercise 24-1

AI: Explainer

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

Exercise 24-2

AI: Explainer

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

Exercise 24-3

AI: Explainer

for base in 'ATGC':
print(base)

Exercise 24-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 24-5

AI: Explainer

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

Exercise 24-6

AI: Explainer

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

Exercise 24-7

AI: Explainer

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

print(average_length([]))

Exercise 24-8

AI: Explainer

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

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

Explaining what it does

Here is some code that runs. Predict the output each code block generates. Then ask the assistant what it the code does and what why it ends up printing what it does. Compare to your prediction.

Exercise 24-9

AI: Explainer

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

Exercise 24-10

AI: Explainer

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

Exercise 24-11

AI: Explainer

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

Bring your own blunders

Exercise 24-12

SOLO

Find an error message you got yourself working on code this week. One that confused you at the time. Write down what you think was happening using what you have learned since then.

Exercise 24-13

AI: Explainer

Ask the assistant to explain the same error. Compare its explanation to the one you wrote yourself. Confirm both explanations against the actual running code. Which explanation was was better and most correct?