Course contents · Chapter 3
Building with AIChapter 3 of 42 min reading + practice

Process each record with an explicit rule

A loop repeats work over a collection; a condition chooses which branch applies.

A loop repeats work over a collection; a condition chooses which branch applies.

Trace one iteration at a time before generalising. Include values that pass and fail the condition and an empty collection.

A boundary is part of the requirement

The condition attendees < 6 is true for five and false for six. Using <= changes the requirement to include six. When a programme specification says fewer than, at least, above or no more than, translate that language into a comparison and test the boundary deliberately.

A for loop assigns each item from a collection to the loop variable and runs the indented block for that item. If the collection is empty, the body runs zero times. If a print statement is placed outside the conditional but inside the loop, it runs for every record rather than only the matching records.

Create records with attendance 5, 6 and 8. Predict which names print, then run the program. Change < to <= and explain the difference. Finally, use an empty list. None of these tests establishes that negative attendance or missing fields are handled correctly; add those conditions only after defining their intended behaviour.

The bigger picture

A Python program transforms values according to explicit operations. To understand a program, trace the values and their types, not just the printed output. Text that looks like a number remains text until an operation converts it.

  1. Read inputs
  2. Trace operations
  3. Run code
  4. Test another case
A working model for this course. Use it to locate where a decision or failure occurs.

Concepts to keep close

Type
The kind of value, which determines the operations that have meaning for it.
Control flow
The order in which statements execute, including branches and loops.
Return value
A result passed back to calling code; printing a result is a different action.

Reasoning through the problem

Translate 'fewer than six' as a strict comparison.

Trace values 5, 6 and 8.

Record what happens when there are no records.

Use the idea carefully

Predict output before running a short example. Change one value or operation and explain the new result. Test an ordinary case, an empty input and a boundary. Read errors from the final exception message back to the relevant line. A passing example is evidence about that input, not proof that every possible input works.

Further reading & course resources

Original documentation and teaching references for further investigation.