Sub topic 4
Try First, Then Ask: An Attempt-First Workflow
This chapter assumes you can write functions, conditionals and loops in one language, run a script and a simple test from a terminal or editor, and have used a chat-based AI assistant or code completion tool. The examples use Python.
Why the order matters
The earlier chapters covered two ideas. Skill grows through effortful retrieval and productive struggle, and an AI can play several roles: tutor, reviewer or generator. This chapter combines them into one habit. You attempt the problem before the AI says anything about it.
The order matters because of what the first attempt does for you:
- It makes you retrieve what you already know. That retrieval is the practice.
- It shows you which part of the problem you don't understand. Without an attempt, you can't tell "I don't know how to start" from "I have one wrong assumption".
- It gives the AI something concrete to respond to. A question like "why does my code return 1 for this input?" gets a much better answer than "write a median function".
- It makes you read the AI's answer as a comparison with your own thinking. You are no longer just reading text that looks plausible.
An attempt-first workflow is not a rule that you must never ask for help. You ask later and in smaller steps, and you stay in charge of the problem.
The workflow at a glance
The loop has five parts: prepare, attempt, diagnose, ask in small steps, and close the loop. The sections below take them in order.
Step 1: Restate the problem and predict
Before you write code, say in one or two sentences what the function receives, what it returns, and one or two example input and output pairs. If you can't do this, you don't yet understand the task, and the first question to ask is about the task, not the code.
Prediction is a cheap way to train your mental model of how code behaves. Read the code below and write down what you expect it to print before you run it.
def count_vowels(text):
return sum(1 for ch in text.lower() if ch in "aeiou")
# Predict the output before you run this line.
print(count_vowels("Learning with AI"))The expected answer is 6: the vowels are e, a, i, i, a, i. If your prediction differs from the output, don't just accept the output. Work out which step of your reading was wrong. Did you forget lower(), so that you skipped the capital "A" and "I"? Did you miscount? The gap between prediction and result is where the learning happens.
Step 2: Make a timeboxed attempt
A timebox is a fixed period you commit to before asking for help. For a small exercise, 15 to 20 minutes is a reasonable start. For a larger task, split it into pieces and timebox each piece.
During the attempt:
- Write something, even if it is wrong or incomplete. A partial attempt is more useful than a blank file.
- Run it early. Use
printcalls or a debugger to see what the code actually does. - Keep notes on what you have tried. Two lines per attempt are enough.
- Look up documentation if you need it. Documentation is a different kind of help from a generated solution, because you still have to apply it yourself.
The timebox exists to stop two opposite failures. If it is too short, you ask before you have struggled enough to learn anything. If it is too long, you spend an hour on a typo and become frustrated. Struggle is useful only while you are still making progress, even slow progress. If you have been repeating the same failed idea for ten minutes, stop and move on to step 3 without waiting for the timer.
Step 3: Diagnose where you are stuck
Before you open the AI chat, write a short stuck note. It should answer four things:
- What did I expect the code to do?
- What does it actually do? Include the exact error message or wrong output.
- What have I already tried?
- What is my best guess about the cause?
The fourth item is the one learners skip. A guess that turns out wrong still teaches you something, and writing it down turns a vague feeling of being lost into a hypothesis you can test.
Step 4: Ask for the smallest help that unblocks you
Treat help as a ladder. Start on the lowest rung and climb only when needed.
| Rung | What you ask for | Example | What you keep |
|---|---|---|---|
| 1 | A guiding question | "Ask me one question that helps me find the problem." | Finding the cause |
| 2 | A hint | "Point me to the part of the code to look at, without fixing it." | Writing the fix |
| 3 | An explanation of a concept | "Explain how `sorted` differs from `list.sort`." | Applying the concept |
| 4 | A partial example on a different problem | "Show a similar bug in a different function." | Transferring the idea |
| 5 | Review after you fix it | "Review my working code and tell me what to improve." | Deciding what to change |
| 6 | A full solution | "Show me a complete version." | Studying and re-deriving it |
Rung 6 is allowed, but it comes last and has conditions. After reading a full solution, close it and rewrite it from memory later, and compare it with your attempt. Treat it as an answer to study, not code to paste.
Here is a prompt template that puts you on rungs 1 to 5. Paste your own code and the error or wrong output where indicated.
I am learning Python. I tried to write a function that returns the median of a list, and my code is below.
<paste your code and the error or wrong output>
Do not give me the full solution. First, ask me one question that helps me find the problem. If I am still stuck, give a hint. After I fix it, review my code and tell me what to improve.The prompt sets the AI's role (tutor, then reviewer), the order of help, and the one thing it must not do. Later chapters cover prompt design in more detail.
Worked example: a median function
Suppose the task is: write median(values), which returns the middle value of a list of numbers, or the average of the two middle values if the list has even length.
Your first attempt looks like this:
def median(values):
mid = len(values) // 2
if len(values) % 2 == 1:
return values[mid]
return (values[mid - 1] + values[mid]) / 2
print(median([3, 1, 2]))You expected 2, and the script prints 1. Your stuck note might read: "Expected 2, got 1. I tried [1, 2, 3] and it worked. My guess is that something depends on the order of the input."
That guess is already close. You send the note with the prompt template. A good tutor reply is a question such as: "For [3, 1, 2], which element does values[mid] return, and is it the middle value in sorted order?" Answering it, you see that the code picks the middle position, not the middle value, and that the list has to be sorted first. You make the fix yourself.
You then write your own tests. Ask for review only after they pass. For a function of this shape, the cases below cover odd length, even length, one item, and empty input.
def median(values):
if not values:
raise ValueError("median() requires at least one value")
ordered = sorted(values)
mid = len(ordered) // 2
if len(ordered) % 2 == 1:
return ordered[mid]
return (ordered[mid - 1] + ordered[mid]) / 2
assert median([3, 1, 2]) == 2
assert median([4, 1, 3, 2]) == 2.5
assert median([5]) == 5
try:
median([])
except ValueError:
pass
else:
raise AssertionError("median([]) should raise ValueError")Your first attempt would also have failed on the empty list, but in a less helpful way. With len(values) == 0, the even branch runs and values[-1] raises an IndexError, which doesn't say what the problem is. The reviewer step is where the AI can point this out. You still decide whether to raise ValueError, return None, or do something else. That is a design choice, and it is yours.
Step 5: Close the loop
Finishing the code is not the end of the workflow. Spend a few minutes on three things:
- Summarize the cause. Write one sentence, such as "A median needs sorted data; I used position in the unsorted list."
- Save a test. Keep the test that exposed the bug, so the bug cannot return unnoticed.
- Redo it later. A day or two later, write the function again without looking. If you can't, you have found something to practice.
The summary and the later redo are small, and they are what turns one solved problem into skill you can reuse.
Common errors
- Asking before attempting. The most common failure. The fix is to commit to a timebox and write the stuck note before opening the chat.
- A hollow attempt. Writing a line or two so you can say you tried. The attempt should be a real effort that you could explain.
- Hints that become solutions. If you ask for a hint and the AI returns finished code, you have skipped rungs 1 to 4. Remind it of the rules, or ignore the code and use only the explanation.
- Accepting a fix you don't understand. If the AI's explanation doesn't make sense to you, say so and ask for a smaller step. Don't continue until you can explain the cause.
- Never leaving the ladder's bottom rungs. Some learners always ask for a full solution "just to check". Checking your answer with tests is better, and is the subject of a later chapter.
- Struggling too long. Productive struggle has a limit. If you've made no progress for ten minutes, a hint is the right move.
Edge cases
Some situations need adjustment:
- Unfamiliar tools. If you don't know a framework or command-line tool, a first attempt is not possible in the same way. Start by asking for an explanation of the concepts, then attempt a small task with it.
- Setup and environment errors. Installation problems teach little about programming. Asking for direct help is reasonable here, since the goal is to get to the real exercise.
- Time pressure at work. If you must ship, you may use the AI as a generator. Keep that separate from practice, and still read and test what it gives you.
- Very hard problems. If the problem is far above your level, an attempt will not get far. Lower the difficulty, or ask the AI for a simpler related problem and build up.
Exercise
You wrote a function that returns the wrong output for one input. You have spent 25 minutes and are stuck. List the steps you take before you ask the AI for a full solution, in order.
Show answer
First write a stuck note covering the expected result, the actual result with the exact error or output, what you have tried, and your best guess about the cause. Then ask for the lowest rung of help, such as one guiding question, and a hint if that is not enough. Fix the code yourself, test it with your own cases, and only then ask for a review. Ask for a full solution only if the earlier rungs fail, and then rewrite it from memory later.
Exercise
Predict what count_vowels("Learning with AI") prints, then run it. Explain what would change if the function did not call text.lower().
Show answer
It prints 6, because the vowels are e, a, i, i, a, i. Without text.lower(), the capital "I" in "AI" and the capital "A" would not match the lowercase string "aeiou", so the function would print 4.