BeginnerCoder

Lesson 14 — Comparing two AI answers with tests

Let the tests decide

Ask the same question twice — or ask ChatGPT and Claude — and you will often get two different answers. Which one is better? Not the longer one, not the one that sounds more sure. The one that passes more of your tests.

A test suite is a list of test cases you run against any function:

def count_ok(func):
    ok = 0
    for inp, expected in EXAMPLES:
        if func(inp) == expected:
            ok += 1
    return ok

Because functions are values in Python, you can pass answer_a or answer_b into the same run_suite and compare the scores. Print the failures too — they tell you why an answer lost, and they are exactly what to send back to the assistant.

Why it matters: the same suite works for every answer, every retry and every assistant. It turns "I think B is better" into a number.

Want it another way?
Task

Finish run_suite(fn): for each case, call fn(given); count passes, and print "FAIL name(given) -> got, want want" for failures. Return the number passed.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Coding with AI: ChatGPT & Claude

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.