Lesson 4 — Read AI code before you run it
Predict, then run
Running code you do not understand is how bugs (and worse) get into your project. Before you run an AI answer, read it and predict the result.
The simplest way is a trace table: go through the loop by hand and write down every variable after each step.
| n | skip? | total | out |
|---|---|---|---|
| 3 | no | 3 | [3] |
| -1 | ? | ? | ? |
If your prediction matches the real output, you understand the code. If it does not, you just found the part you need to read again — or a bug.
Watch out: comments written by the assistant describe what it meant to do. Trust the code, not the comment.
Why it matters: reading code is most of a programmer's job, and with an assistant writing drafts it becomes even more of it.
Trace ai_running_total([3, -1, 4, 2]) by hand and set my_prediction to the list you expect. Then run it.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.