0%

Plan, Build, and Test Programs · practice

Work Through Examples by Hand

Before you ask Python for an answer, work through a few answers yourself. A hand-worked example turns a requirement into something concrete.

The quiz score starts at zero and gains one point for every True result. Here is one result :

[True, False, True]

Trace the score one result at a time:

ResultScore afterward
start0
True1
False1
True2

Write down the expected score, 2, before writing quiz_score().

Why do this first?

If the code later returns 1 or 3, you have a precise disagreement to investigate. Without an independent expected result, it is tempting to trust whatever the program prints.

Working through the score also exposes unanswered questions:

  • Does an empty result list produce a score of zero?

  • Does each correct answer add exactly one point?

  • Can the score ever be greater than the number of results?

Those questions belong in the requirements. Discovering them before the implementation is cheaper than discovering them after several depend on an assumption.

Use more than one shape of example

Choose a small set with different purposes:

  • a normal case;

  • a zero or empty case when it is allowed;

  • an important boundary;

  • a case where every answer is correct or every answer is incorrect.

For the quiz score, an empty list checks the starting . A list of all True values checks that every correct answer is counted. Each example has a job.

ResultsExpected scorePurpose
[True, False, True]2normal mix
[]0empty case
[False]0no correct answers
[True, True, True, True]4every answer correct

Keep expected results independent

Do not calculate an “expected” result by calling the function you are testing:

expected = quiz_score([True, False, True])

That only asks the same code to agree with itself.

Instead, record the result you worked out from the requirement:

expected = 2

The example is useful because it came from a separate line of reasoning. Letting a function invent its own expected result is a little like letting it grade its own homework.

Why should an expected result be calculated independently from the function under test?

Task

Plan and implement quiz_score(results).

The rule is:

start at zero and add one point for every True result

The starter contains three example . Before implementing the , trace each result by hand and replace its None with the expected score.

Then implement the function and keep the that prints the three scores. The final output must be:

2
0
3

Your function must also work for other lists of results.