Programming questions

Taking a question apart, the jobs that come up again and again, and how the marks are given.

F13.4Exam preparationGCSE20 min

Do this lesson in the simulator

The biggest marks on the programming paper come from writing a program to a description. Those questions are predictable: the same handful of jobs come round every year, dressed differently. This lesson is how to take one apart and get every mark, and then you write one.

Read the question like a specification

Underline, in the question:

  • every input, and where it comes from;
  • every output, and its exact wording;
  • the rules: ranges, what counts as valid, what to do when it is not;
  • the edge cases: empty, zero, the first one, the last one.

Then write the structure before the detail: input, then process, then output.

The jobs that come up again and again

The job What it needs
Validate an input a loop that keeps asking, a check, a message for each kind of failure
Count things that match a counter set to 0 before the loop, + 1 inside an if
Add up and average a running total, then divide by how many, watching for zero
Find the largest or smallest a best-so-far variable, replaced when something beats it
Search a list a loop, a found flag or an early return
Read a file into a list open, loop over the lines, strip, split, close
Build a report a loop that prints one line each, then totals at the end

Every one of those is a lesson you have already done. The exam is those bricks, rearranged.

How the marks are given

Marks are for what the program does, broken into steps, not for it running perfectly. So:

  • write something for every part, even if an earlier part is unfinished;
  • use meaningful names: a marker reading total understands your code faster than one reading t;
  • comment the tricky line, not every line;
  • handle the failure case the question mentions: that is always a mark;
  • if you cannot finish, write what is left in a comment as pseudocode. Examiners give marks for a correct approach.

Worked example

Ask the user how far to drive, in centimetres. Accept only a whole number from 5 to 60. Drive that far in steps of 5 cm, printing the distance so far after each step. Then print the total.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

# input, and validate it: keep asking until the answer is a whole number in range
answer = "25"                       # in the exam this is input(); fixed here so the page can run
while not (answer.isdigit() and 5 <= int(answer) <= 60):
    print("Enter a whole number from 5 to 60")
    answer = "25"
far = int(answer)

# process: drive in 5 cm steps, counting as we go
done = 0
while done < far:
    step = min(5, far - done)       # the last step may be shorter
    forward(50, distance=step)
    done = done + step
    print("so far:", done, "cm")

# output
print("total:", done, "cm")

Run this in the simulator

Notice the last step: the question said steps of 5 up to the distance, and 25 divides by 5, but a good answer copes when it does not.

Task: an exam-style program

Write the program for this question. The robot must drive far centimetres in steps of step centimetres, where the last step is whatever is left over. After each step, print so far: <n> cm. At the end print steps: <n> and total: <n> cm. Use the values given; do not type the answers.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

far = 32
step = 7

Challenges

  1. Change far to 30 and step to 5. Does your program still print the right number of steps?
  2. Add validation: refuse a step of 0 and say why, instead of looping forever.
  3. Rewrite it with a for loop instead of a while loop. Which reads better here?