Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Flashcards: open the module flashcards (end-of-module review with workplace questions).

Online Courses / University Track / Python from Zero

Module 8 · Lesson 1 · Core

Planning a useful program

Start with the user, the inputs, the outputs and a plan of functions, all on paper.

  • 55 minutes
  • Serves O6
  • Assumes Module 7

Phase 1: Prepare

Step 1 Warm-up recall

From Module 7: what is the first step of debugging?

Private: nothing you type here is sent or stored.

Step 2 Learning goal

By the end of this lesson you can:

  • state the user, need, inputs and outputs (Foundation);
  • work an example by hand (Core);
  • split the task into functions (Core).

Phase 2: Teach

Step 3 A familiar example

An architect draws plans before any bricks are laid, and the client agrees to them. A program is planned the same way, on paper, before any code is typed.

Step 4 Clear explanation

Choose a small, useful program: a stock report, a marks summary or a weekly spending total. Plan it in five answers.

Needwho and whyInputsfiles, valuesStepsfunctionsOutputsreport
Start from the user and the result: who needs what, from which data, in what form.
  1. User and need: who will use it, and what decision will it help with?
  2. Inputs: which file or values, in what form, with which known problems?
  3. Outputs: what exactly will it show or write, and where?
  4. A worked example: a tiny input and the output you expect, worked out by hand. This becomes your first test.
  5. Functions: one job each, with a name and a stated unit.
# Plan: stock report
# Need: shop owner wants items below reorder level
# Input: stock.csv with columns item,qty,reorder
# Output: list of items to reorder, and a count
#
# Functions:
#   load_rows(path)    -> rows, bad_count
#   needs_reorder(row) -> True or False
#   make_report(rows)  -> list of item names
#
# Example: pen,3,5 -> reorder; ink,9,5 -> fine

The plan is short, but it already settles what the program does and does not do. If a user asks for something outside it, you can say so clearly, and add it later as a new function.

Step 5 Worked example (4 marks)

A stock file has the rows pen,3,5, ink,9,5 and pad,5,5. Using the rule "reorder when qty is at or below the reorder level", what should the report list?

  1. pen: 3 is below 5, so reorder. (1 mark)
  2. ink: 9 is above 5, so fine. (1 mark)
  3. pad: 5 equals 5, which is at the level, so reorder. (1 mark)
  4. Answer: pen and pad; pad is the boundary case. (1 mark)

Go deeper: the professor's notes

Plan first: who needs it, what goes in, what comes out, and a worked example.
User

Who and why.

In

Files.

Out

Report.

NeeduserInputfileStepsfunctionsOutputreport
Plan

Plan

QuestionAnswer
who uses itthe shop owner
what comes outitems to reorder

At work: A stock report

An owner wants to know what to reorder.

  1. Write the five answers.
  2. Work one example by hand.

What a careful practitioner decides. A paper plan costs far less than a code change.

Phase 3: Practise

Step 6 Learner practice (instant-marked)

1. The first thing to settle in a plan is:

2. A worked example done by hand becomes:

3. Each function in the plan should have:

4. Why plan on paper first?

Phase 4: Close

Step 7 Recap

  • Name the user, need, inputs and outputs.
  • Work a small example by hand as your first test.
  • Split into functions with one job each.

Step 8 Exit ticket (ungraded)

Write the user, need, input and output for a program you would find useful.

Private: nothing you type here is sent or stored.

Step 9 Go further (Extension)

Draft the function list for your capstone program and one hand-worked example.

In industry: Heathrow Terminal 5: test filters left in place

During testing of the baggage system at Heathrow's Terminal 5, technicians installed software filters to stop test messages reaching live systems. They were accidentally left in place after the terminal opened on 27 March 2008, so some messages about transferring bags were not received. In the first five days more than 23,000 bags were misplaced and 500 flights were cancelled.

Link to this lesson. Remove test settings before release and check what the live system receives.

Source: Computer Weekly, 14 May 2008; House of Commons Transport Committee, The Opening of Heathrow Terminal 5

Common mistakes

MistakeWhy it fails
Coding before planningPlan inputs, outputs and an example first.
A plan too big to finishChoose a small, useful program.
No worked exampleYou will have nothing to test against.

Limits. This lesson plans a program of a few functions; larger systems need more design and a team.

Flashcards for this lesson (3)

Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.

Card 1 of 3 · What makes a program useful?
NeedrealRuneasilyTrusttested
Useful

Someone needs it, can run it, and can trust it. A stock report that the shop owner uses every week is useful; clever code nobody can run is not.

Card 2 of 3 · What should a plan contain?
NeeduserDatain and outStepsfunctions
The plan

The user and need, the inputs, the outputs, a worked example and a list of functions. Written on paper before any code.

Card 3 of 3 · Why work an example by hand first?
SmallinputBy handanswerBecomesfirst test
Worked example

It gives you a known answer to test against, and it often shows a missing rule, such as what happens at exactly the reorder level.

Review with flashcards

Test yourself on this module with workplace flashcards and detailed answers.

Open the module flashcards