OCR GCSE Computer Science June 2025 Paper 2, Question 5(d): validating a PIN

OCR J277/02 June 2025, Question 5(d): create an algorithm that takes a PIN as a string, checks that it is 4 characters long and is not 1234 or 4321, and outputs VALID PIN or INVALID PIN. A model answer, the six marks explained, and the check to run.

Past paper questionOCR J277/02June 2025 Paper 26 marksWrite a program

Question 5(d) of the OCR GCSE Computer Science Paper 2 sat on 20 May 2025 (J277/02) is the 6 mark algorithm in Section A. It is a length check and a "must not be" check joined together, and the join is where it goes wrong.

We do not copy the exam paper here. Open it beside this page: OCR June 2025 J277/02 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

A user chooses a PIN. Create an algorithm that:

  • takes the PIN as input, as a string;
  • checks that it is 4 characters long;
  • checks that it is not "1234" or "4321";
  • outputs "VALID PIN" if it passes, and "INVALID PIN" if it does not.

You do not need to check that the characters are digits.

A model answer

In OCR Exam Reference Language:

pin = input("Enter PIN")
if pin.length == 4 and pin != "1234" and pin != "4321" then
    print("VALID PIN")
else
    print("INVALID PIN")
endif

In Python:

pin = input("Enter PIN: ")
if len(pin) == 4 and pin != "1234" and pin != "4321":
    print("VALID PIN")
else:
    print("INVALID PIN")

AND, not OR

A valid PIN is not 1234 and not 4321. Try or: pin != "1234" or pin != "4321". For the PIN 1234, the first half is false but the second half is true (1234 is not 4321), so the whole thing is true and 1234 is accepted. With or, the check never rejects anything.

If you test for bad PINs the words flip: pin.length != 4 or pin == "1234" or pin == "4321" means INVALID.

Where the six marks are

  • inputting the PIN and storing it;
  • using selection;
  • checking for 4 characters;
  • checking for both 1234 and 4321;
  • "INVALID PIN" output for every invalid PIN;
  • "VALID PIN" output only for a valid PIN, with nothing contradictory printed as well.

Where the marks are lost

  • pin != "1234" or "4321". Each comparison must name the variable. The mark scheme says the check must refer to the variable both times.
  • or between the two "not" checks. See above.
  • Three separate ifs that each print. Then 1234 prints INVALID PIN for one check and VALID PIN for another. The last mark needs VALID PIN to be the only output.
  • Treating the PIN as a number. The question says it is a string. An integer has no length, and 0042 as an integer is 42. (The mark scheme lets this go. Real code would not.)

Run it

The check as a function, run on five PINs. Swap the ands for ors and watch every PIN pass. The robot's light shows the last result.

7291 is VALID. 1234, 4321, 123 and 12345 are INVALID. Then type a PIN of your own.
The program
from bugbot import *
connect()

def check(pin):
    if len(pin) == 4 and pin != "1234" and pin != "4321":
        return "VALID PIN"
    else:
        return "INVALID PIN"

for pin in ["7291", "1234", "4321", "123", "12345"]:
    print(pin, check(pin))

pin = input("Enter PIN: ")
result = check(pin)
print(result)
if result == "VALID PIN":
    led("green")
else:
    led("red")
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 OCR J277 June 2025 Paper 2 Question 5(d)?

Input the PIN as a string. If its length is 4 and it is not "1234" and it is not "4321", output VALID PIN. Otherwise output INVALID PIN.

What is a length check?

Validation that makes sure the input has the right number of characters, such as exactly 4 for a PIN.

Why should a PIN be stored as a string?

A PIN can start with 0, and an integer would drop the leading zero. A string also has a length that can be checked, and no arithmetic is ever done on a PIN.

More from this paper

Every OCR J277 question we have worked · Guide: Logic gates and truth tables explained

Learn it step by step

  1. F6.1 Defensive design and validation Robust programs
  2. F3.1 String handling Strings, lists and records
  3. F2.3 else, elif and Boolean operators Decisions and loops
Open the lessons

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