Skip to content
mentoor

Topic 2 of 5 · 4 min read

Predicting code before asking for an explanation

Goal: Predict what a code snippet does, then use AI to check the prediction and explain where your guess differed.

Before you ask an AI what a piece of code does, write down your own guess, then use the AI to check the guess and explain exactly where it differed.

Check yourself · 1 of 3

Why write a guess before asking an AI to explain a snippet?

Choose one answer

Why it matters

When you paste code and ask "explain this", the explanation reads smoothly and you feel you understood. That feeling is not evidence. You never had to commit to anything, so you cannot tell what you actually knew. Prediction fixes that. A guess that turns out wrong shows you the gap you would otherwise have skipped over, and a gap you found yourself sticks better than one a chatbot narrated to you.

This works for any language and any tool: ChatGPT, Claude, Gemini, Copilot chat or an editor plugin.

How to do it

  1. Read the snippet and write your guess in plain words. One or two sentences: what goes in, what comes out. If you can, give a concrete result for one input.
  2. Run it if you can. Running the code beats any explanation. If it runs, compare the real output with your guess before you ask the AI anything.
  3. Send both to the AI. Paste the code and your guess. Ask it to say what is right and wrong in your guess, and to explain the difference line by line.
  4. Ask for a new input. "Give me an input where my guess would be wrong, and I will predict the output." Repeat once.
  5. Write the rule in your own words. One sentence you could tell a friend.

Before and after

Before: Explain this code to me.

You get a tidy explanation and move on.

After: Here is a snippet. My guess: it returns the list sorted from smallest to largest. Tell me which parts of my guess are right or wrong, and explain the difference line by line. Do not rewrite the code.

For engineers, a small example. A learner guesses that this sorts words by length:

words = ["pear", "fig", "banana"]
result = sorted(words, key=lambda w: w[-1])
print(result)

The guess is wrong. The key is the last letter, not the length, so the result is ['banana', 'fig', 'pear']. Because the learner wrote the wrong guess down, the correction is clear: w[-1] means "last character".

What goes wrong

  • Peeking first. If you read the AI's explanation before you guess, you are only recognising, not predicting.
  • Vague guesses. "It processes the data" cannot be wrong, so it teaches nothing. Say a result.
  • Trusting the explanation. Models explain confidently and sometimes wrongly, especially for rare libraries or edge cases. When the explanation says what a function does, run it or check the official docs.

Examples

A guess that can fail

Snippet: total = sum(x for x in nums if x % 2)

Weak guess: It adds up the numbers.

Strong guess: For [1, 2, 3, 4] it returns 6, because it keeps the even numbers 2 and 4.

The strong guess is specific enough that you can check it. The condition x % 2 is true for odd numbers, so the real answer is 4 (1 + 3), not 6. Your guess was wrong, and you now know why.

A follow-up that tests the rule

After the AI corrects you, ask: "Give me one list where my corrected rule would still fail, and let me predict the result first." This catches a half-fixed idea before it settles.

Key points

  • Writing a guess before asking for an explanation shows you which parts you really understand.
  • A good guess names a concrete result for a concrete input, so it can be wrong.
  • Run the code when you can, because running it beats any explanation.
  • Ask the AI to compare your guess with the code line by line instead of rewriting it.
  • Check an explanation of an unfamiliar library against its official docs.

Common mistakes

  • Reading the AI's explanation before writing a guess.
  • Guessing so vaguely that no result could prove you wrong.
  • Skipping the run step when the code is easy to run.
  • Believing the explanation of an unfamiliar library without checking the docs or running it.

Check yourself · 2 of 3

Which guess about a snippet is best to send to an AI for checking?

Choose one answer

Check yourself · 3 of 3

The AI says your guess was wrong. What is the most useful next step?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic