Ask It Simply
Build With AI · Advanced · Lesson A3

Build with Grok as your coding partner

about 10 minutes. Level: Beginner. Nothing to install: copy-paste Grok prompts plus one Google Colab notebook.

Watch the video

Lesson video, about 4 min 39 s. Captions are off by default. Press CC to turn them on. It starts muted. Press Sound to hear the lesson from the start. Press and hold the playing video (or hold the period key) to watch at 2x speed.
Show transcript

Welcome to Lesson A3. Today Grok becomes your coding partner. You'll learn how to ask for code the right way, how to make Grok prove its code works, and how to fix a bug together. Everything runs in your browser.

Let's start with the most important idea. Our tip: you're the boss, and Grok is the builder. Grok writes the drafts. You review them, test them and own them. If you can't explain what a line does, ask Grok to explain it before you use it.

Here's the build loop we'll use. Prompt, then run, then test, then fix. Then repeat until it works.

Step one is the prompt, and the best prompts start with a spec. Spec is short for specification. It's a short description of what the code must do. A clear spec gets better code. A fuzzy request gets fuzzy code.

Here's our spec. Copy it from the lesson page and paste it into Grok. The goal is to add up a shopping list. The input is a list of items, each with a name, a price and a quantity. The output is the total, rounded to two decimal places. And one rule: if a quantity is zero or less, skip that item.

Then we ask for something many beginners forget. Tests. Three of them. A normal list, an empty list, and a list with a quantity of zero. And we ask for everything in one block we can paste into Colab, plus a plain-words explanation.

Here's Grok's reply. There's the code, with comments. There are the tests. And below, an explanation in plain words. Take a moment to read it. Does each part make sense?

Now step two, run. Open Google Colab, start a new notebook, and paste Grok's code into one cell. Press Run.

It prints All tests passed, and there's no red error. That's good news. The tests use a word called assert. An assert stays quiet when things are right. It only complains when something is wrong. So reaching that last line means all three tests passed.

Now let's practice the most useful skill in coding with AI, fixing a bug. I'll break the code on purpose. First I add a fourth test from the lesson page, with a quantity of minus one. Then I delete the two lines that skip items with a quantity of zero or less, the if line and the continue line. Press Run.

There it is. A red Assertion Error. One of our tests caught the problem. This is exactly why we asked for tests.

Step four, fix. Copy the whole error message. Go back to Grok and paste it with the fix prompt from the lesson page. We ask Grok to explain what went wrong in plain words, then give us the fixed code.

Look for Grok to explain that items with zero quantity weren't being skipped, and to give you the fixed code. Paste it in, press Run, and the error is gone. You just did the full loop. Prompt, run, test, fix.

Why does testing matter so much? Anthropic's agent guide warns about, quote, "the potential for compounding errors." End quote. That means one small mistake can grow into bigger ones. The same guide recommends, quote, "extensive testing in sandboxed environments, along with the appropriate guardrails." End quote.

Our tip: for anything important, get a second opinion. Paste the code into ChatGPT or Claude and ask them to review it for bugs, without rewriting it. Two reviewers catch more than one.

And when you're ready for bigger projects, Cursor describes itself as, quote, "the AI editor and coding agent." End quote. That's a next step for later. For now, Grok and Colab are all you need.

Safety check. Our tip: AI code can look right and be wrong, so run the tests every time. And never paste secrets, like keys, passwords or account numbers, into a chat with any AI.

Quick check. What's a spec? A short description of what the code must do. What's the build loop? Prompt, run, test, fix. What do you do with an error? Paste it back to Grok and ask it to explain and fix. Next, in Lesson A4, you'll give an agent its first tool.

What you'll learn

Step 1: You're the boss, Grok is the builder

Our tip: Grok writes the drafts. You review them, test them and own them. If you can't explain what a line does, ask Grok to explain it before you use it.

The build loop:

Prompt, then Run, then Test, then Fix

Step 2: Write a "spec" first

A spec (short for specification) is a short description of what the code must do. Clear specs get better code. Copy this into Grok:

You are my coding partner. Write simple Python for a beginner.

Spec:
- Goal: add up a shopping list.
- Input: a list of items, each with a name, a price and a quantity.
- Output: the total cost, rounded to 2 decimal places.
- Rules: if a quantity is 0 or less, skip that item.
- Keep it under 20 lines, with a comment on each part.

Also write 3 tests using plain assert lines:
1. A normal list.
2. An empty list (total should be 0).
3. A list with a quantity of 0.

Put the code and tests in ONE block I can paste into Google Colab.
After the code, explain each part in plain words.

Step 3: Run it in Colab

  1. Open colab.research.google.com and start a new notebook.
  2. Copy Grok's code block, paste it into one cell and press Run.
  3. If it prints All tests passed and there's no red error, the tests passed. An assert only complains when something is wrong.

Grok's real answer (our test run)

We ran this exact prompt through Grok (model grok-4.7) on Oct 30, 2026. Grok's answers change from run to run, so yours will differ. Quoted as Grok returned it, except where we mark [trimmed] and where we swapped dashes for commas to match our style.

# A shopping list item is a name, a price, and a quantity.
def total_cost(items):
    # Start the running total at zero.
    total = 0
    # Look at each item in the list.
    for name, price, quantity in items:
        # Skip this item if the quantity is 0 or less.
        if quantity <= 0:
            continue
        # Add price times quantity to the total.
        total = total + price * quantity
    # Round to 2 decimal places and return the total.
    return round(total, 2)

# Test 1: a normal list should add up correctly.
assert total_cost([("apple", 1.50, 2), ("bread", 2.25, 1)]) == 5.25
# Test 2: an empty list should total 0.
assert total_cost([]) == 0
# Test 3: an item with quantity 0 should be skipped.
assert total_cost([("milk", 3.00, 0), ("eggs", 2.00, 1)]) == 2.00
print("All tests passed")

What each part does

We ran Grok's code in Python on our computer. It printed All tests passed.

Step 4: Break it, then fix it with Grok

Let's practice fixing a bug. First, add this fourth test under the other three: assert total_cost([("milk", 3.00, -1), ("eggs", 2.00, 1)]) == 2.00. It checks that a negative quantity is skipped too. Then, in your cell, find the two lines that skip items with a quantity of 0 or less (the if quantity <= 0: line and the continue line under it), and delete both. Press Run. You should see a red AssertionError.

Now copy the whole error message and paste it into Grok with this:

My test failed. Here is the error. Explain what went wrong in plain words,
then give me the fixed code in one block.

[paste the error here]

Why the fourth test? In our test run, deleting the skip line did not break the first three tests, because a quantity of 0 adds 0 to the total anyway. When we tried the fix prompt, we sent Grok the lesson's working code with its 3 tests and our old "delete the line" step. We did not send it an error message. Grok's reply contradicted itself. It said the third test fails, then said that test may not fail, and that a quantity of -1 would. It also asked us for the error instead of giving fixed code. So check AI answers against what you actually see. That's why we added the fourth test. Our tip: good tests check the tricky cases.

Paste Grok's fix into Colab and run it again. When the tests pass, you've done the full loop.

Step 5: A second opinion (optional)

Our tip: for anything important, paste the code into ChatGPT or Claude and ask, "Review this code for bugs. Don't rewrite it, just list problems." Two reviewers catch more than one. When you're ready for bigger projects, Cursor calls itself "the AI editor and coding agent." (Cursor docs) It's a next step for later.

Why tests matter

Anthropic's agent guide warns that agents bring "the potential for compounding errors." (Anthropic, "Building effective agents") The same guide recommends "extensive testing in sandboxed environments, along with the appropriate guardrails." Our tip: for a beginner, a test is the fastest way to catch a mistake before it grows.

Check yourself

  1. What is a spec? (Answer: a short description of what the code must do)
  2. What are the four steps of the build loop? (Answer: prompt, run, test, fix)
  3. What do you do with an error message? (Answer: paste it back to Grok and ask it to explain and fix)

Caution box Our tip: AI code can look right and be wrong. Run the tests every time. Never paste secrets (keys, passwords, account numbers) into a chat with any AI.

Builds on

Sources

Creator credits

Keep going