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.
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:
if year < 1: too small.elif year > 13: too big. This must come before the stage tests, or 14 would be College.elif year < 7: Primary.elif year < 12: Secondary. This must come after< 7, or 3 would be Secondary.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 < 12beforeelif year < 7. Every primary year is also below 12, so Primary would never print.- Broken indentation. The
elifs line up with theif. The body of the loop is indented under thewhile. - 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.
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")
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
- Question 1: Roll a dice: import random, a variable, a constant, randint and a print 7 marks
- Question 3: Odd or even from 1 to 20: a runtime error, logic errors and messages 13 marks
- Question 5: The volume of a cone from a subprogram, printed to three decimal places 15 marks
- Question 6: A login search of a sorted 2D list with passcode validation and an early stop 15 marks
Every Edexcel 1CP2 question we have worked · Guide: Logic gates and truth tables explained
Learn it step by step
- F2.3 else, elif and Boolean operators Decisions and loops
- F2.6 Condition-controlled loops: while Decisions and loops
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.