Edexcel GCSE Computer Science sample Paper 2, Question 4: year groups, lines mixed up

Pearson Edexcel 1CP2/02 sample assessment material, Question 4: put the mixed-up lines of a program in order so that it turns a year group into Primary, Secondary or College, rejects bad years, and loops until 0. The fifteen marks and the order that matters, with the program to run.

Past paper questionPearson 1CP2/02Sample Paper 215 marksRearrange the lines

Question 4 of Pearson's sample assessment material for Edexcel GCSE Computer Science Paper 2 (1CP2/02) is the "rearrange the lines" question: 15 marks. Unlike the later papers, this one does not keep the indentation for you, so layout is part of the job.

We do not copy the paper or Pearson's code files here. Open the paper beside this page: Edexcel 1CP2/02 sample assessment materials (PDF). The mark scheme is in the same document.

The question in short

The user enters a year group. 1 to 6 is Primary, 7 to 11 is Secondary, 12 and 13 is College. 0 ends the program. Anything below 1 or above 13 gets a validation message. The program loops until 0 is entered.

The order that matters

The lines test with < in a chain, so the order of the tests decides whether they work:

  1. if year < 1: too small.
  2. elif year > 13: too big. This must come before the stage tests, or 14 would be College.
  3. elif year < 7: Primary.
  4. elif year < 12: Secondary. This must come after < 7, or 3 would be Secondary.
  5. else: College.

And the input is read once before the loop, so that a first entry of 0 ends it, and again at the end of the loop.

A finished program

# the stage of education for a year group; 0 to stop
year = int(input("Enter year group (0 to stop): "))

while year != 0:
    if year < 1:
        print("Year too small")
    elif year > 13:
        print("Year too big")
    elif year < 7:
        print("Primary")
    elif year < 12:
        print("Secondary")
    else:
        print("College")

    year = int(input("Enter year group (0 to stop): "))

Where the fifteen marks are

Comments, white space and layout; the first input outside the loop; a while as the outermost loop; > 13 placed after < 1; < 12 placed after < 7; the next input inside the loop; the two validation messages matching their tests; the three stage messages matching theirs; and correct output for 0, for 1 and 6, for 7 and 11, and for 12.

Where the marks are lost

  • elif year < 12 before elif year < 7. Every primary year is also below 12, so Primary would never print.
  • Broken indentation. The elifs line up with the if. The body of the loop is indented under the while.
  • Only one input. With it before the loop only, the loop never ends. With it inside only, a first 0 is not caught.
  • No comments or spacing. A whole mark on this question.

Run it

Enter year groups, then 0.

1 and 6 are Primary, 7 and 11 Secondary, 12 College, 14 too big, -3 too small, 0 stops.
The program
from bugbot import *
connect()

# the stage of education for a year group; 0 to stop
year = int(input("Enter year group (0 to stop): "))

while year != 0:
    if year < 1:
        print("Year too small")
    elif year > 13:
        print("Year too big")
    elif year < 7:
        print("Primary")
    elif year < 12:
        print("Secondary")
    else:
        print("College")

    year = int(input("Enter year group (0 to stop): "))

print("Exiting")
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

How should the tests be ordered in Edexcel sample Paper 2 Question 4?

Too small, too big, then the stages from the lowest year up: less than 7 is Primary, less than 12 is Secondary, else College. Each test relies on the ones above it having failed.

Why does the order of elif tests matter?

Only the first true test runs. A test such as year < 12 is true for a year 3 student as well, so the narrower test, year < 7, has to come first.

Why is the input read twice?

Once before the loop so that the condition can be checked, and once at the end of each pass to get the next value. If it were read only inside the loop, an initial 0 would not stop the program.

More from this paper

Every Edexcel 1CP2 question we have worked · Guide: Logic gates and truth tables explained

Learn it step by step

  1. F2.3 else, elif and Boolean operators Decisions and loops
  2. F2.6 Condition-controlled loops: while Decisions and loops
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by Pearson, and the question paper and mark scheme remain Pearson's copyright. Read them on Pearson's site with the links on this page.