Edexcel GCSE Computer Science sample Paper 2, Question 3: odd or even, fixed

Pearson Edexcel 1CP2/02 sample assessment material, Question 3: fix a runtime error and logic errors so that a program accepts 1 to 20, rejects anything else, and says whether the number is odd or even. The thirteen marks explained, with the program to run.

Past paper questionPearson 1CP2/02Sample Paper 213 marksFix the code

Question 3 of Pearson's sample assessment material for Edexcel GCSE Computer Science Paper 2 (1CP2/02) is 13 marks for fixing a program that almost works. The paper does not tell you the lines this time. It tells you the symptoms.

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 program must accept only whole numbers from 1 to 20, print invalid input for anything else, and otherwise print the number followed by is even or is odd.

The symptoms: entering a valid number such as 4 causes a runtime error; some acceptable numbers give the wrong output or none; a number outside the range is still processed; and the messages do not match the requirements.

What each symptom means

  • Runtime error on 4: the input is still a string. "4" % 2 crashes. Convert with int().
  • Wrong or no output for some numbers: the odd-or-even test is wrong. The test is number % 2 == 0, using the modulus.
  • Out-of-range numbers processed: the range check is missing or its logical operator is wrong, so the odd/even code runs anyway. Put it in the else of the check.
  • Messages: copy them from the paper exactly.

A finished program

number = int(input("Enter a number from 1 to 20: "))

if number < 1 or number > 20:
    print("invalid input")
else:
    if number % 2 == 0:
        print(number, "is even")
    else:
        print(number, "is odd")

Where the thirteen marks are

Seven single marks: int() on the input; modulus for the odd/even test; at least one if; <= 20; >= 1 (or their opposites); the right logical operator joining them; and the two output messages matching the requirements. Then up to 3 for design and 3 for functionality, tested with normal, boundary (0, 21) and extreme data.

Where the marks are lost

  • number / 2 == 0. Division is only 0 for 0. The remainder is %.
  • if number % 2 == 1 for even. A remainder of 1 is odd.
  • Range check with the wrong operator. For bad values it is or; for good values, and.
  • The odd/even test outside the else. Then 25 is rejected and reported as odd.
  • "Is even" with a capital, or "Invalid input". The messages must match.

Run it

Type numbers. The robot's light is green for even, blue for odd and red for invalid.

4 is even, 7 is odd, 0 and 21 are invalid input. 1 and 20 are the boundaries and both are valid.
The program
from bugbot import *
connect()

number = int(input("Enter a number from 1 to 20: "))

if number < 1 or number > 20:
    led("red")
    print("invalid input")
else:
    if number % 2 == 0:
        led("green")
        print(number, "is even")
    else:
        led("blue")
        print(number, "is odd")
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

What is the answer to Edexcel sample Paper 2 Question 3?

Convert the input with int(). If it is less than 1 or more than 20, print invalid input. Otherwise print the number and "is even" if number % 2 == 0, or "is odd" if not.

What causes a runtime error?

Something that only goes wrong when the program runs, such as arithmetic on a string, dividing by zero or an index outside a list. The code is grammatically fine, so it starts, and then stops with an error.

How do I test whether a number is even in Python?

number % 2 == 0. The % operator gives the remainder after division, and an even number leaves no remainder when divided by 2.

More from this paper

Every Edexcel 1CP2 question we have worked

Learn it step by step

  1. F6.4 Debugging logic errors Robust programs
  2. F1.9 Arithmetic operators Programming basics
  3. F6.1 Defensive design and validation Robust programs
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.