Edexcel GCSE Computer Science June 2025 Paper 2, Question 6: keys with a check digit

Pearson Edexcel 1CP2/02 June 2025, Question 6: write a Python program with a subprogram that checks each record in a 2D array: the key must be three or more letters and digits ending in the right check digit, and the signal a real number from 1.0 to 5.0. A model answer and the fifteen marks.

Past paper questionPearson 1CP2/02June 2025 Paper 215 marksWrite a program

Question 6 is the last question of the Pearson Edexcel GCSE Computer Science Paper 2 sat on 20 May 2025 (1CP2/02): 15 marks for a validation program written from requirements. The question insists on at least one subprogram of your own, and the check digit is the part to get right first.

We do not copy the exam paper or Pearson's code files here. Open the paper beside this page: Edexcel June 2025 Paper 2 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

Records in a two-dimensional array each hold a key and a signal reading.

A valid key has at least three characters, is made of letters and digits only, and ends with a valid check digit: add up the digits in the key apart from the last one, and the check digit is the right-hand digit of that sum. So A2B3C5 is valid (2 + 3 = 5) and A7B30 is valid (7 + 3 = 10, right-hand digit 0).

A valid signal is a real number from 1.0 to 5.0 inclusive.

Find and display every invalid record, saying whether the key or the signal is wrong. It must work for any number of records, and it must include at least one subprogram you wrote.

The check digit as a function

def validKey(key):
    if len(key) < 3:
        return False
    if not key.isalnum():
        return False
    total = 0
    for character in key[0:len(key) - 1]:
        if character.isdigit():
            total = total + int(character)
    return str(total % 10) == key[len(key) - 1]

total % 10 is the right-hand digit of the sum. It is compared with the last character of the key, as a string.

A model answer

The file gives you constants for the minimum key length and the signal limits. Use them.

MIN_KEY_LENGTH = 3
MIN_SIGNAL = 1.0
MAX_SIGNAL = 5.0

def validKey(key):
    if len(key) < MIN_KEY_LENGTH or not key.isalnum():
        return False
    total = 0
    for character in key[0:len(key) - 1]:
        if character.isdigit():
            total = total + int(character)
    return str(total % 10) == key[len(key) - 1]

def validSignal(signal):
    return signal >= MIN_SIGNAL and signal <= MAX_SIGNAL

for record in signalTable:
    key = record[0]
    signal = record[1]
    if not validKey(key):
        print("Invalid key:", key, signal)
    elif not validSignal(signal):
        print("Invalid signal:", key, signal)

Where the marks are

Six single marks: a loop over every record; the signal range check with both limits; a way of excluding characters that are not letters or digits; the length check; summing the digits of the key with isdigit() and int(); and two-dimensional indexing of the key and the reading.

Then up to 3 each for design (constants used throughout, the length check done first so a short key cannot cause an index error, subprograms such as an isValid function), good practice (layout, comments, names) and functionality (the user told which field is invalid, not just which record). The paper tells you what to expect: eight invalid keys and four invalid signals.

Where the marks are lost

  • Adding the check digit into the sum. A2B3C5 would then sum to 10 and fail. Stop one short of the end.
  • Comparing a number with a string. total % 10 is an integer and key[-1] is a character. Cast one of them.
  • isalpha() for isalnum(). Keys contain digits. isalpha rejects them.
  • > 1.0 for >= 1.0. The limits are inclusive.
  • No subprogram. The question says at least one. It is a requirement, not a style point.
  • Counting the invalid records but not showing them. Each invalid record must be displayed, with its key and signal.

Run it

A short table with two bad keys and one bad signal. Add records of your own.

It reports A7B31 and A2 as invalid keys and A11 as an invalid signal, and says nothing about the valid records.
The program
from bugbot import *
connect()

MIN_KEY_LENGTH = 3
MIN_SIGNAL = 1.0
MAX_SIGNAL = 5.0

signalTable = [["A2B3C5", 2.5], ["A7B30", 4.0], ["A7B31", 3.0], ["A2", 1.5], ["A11", 5.5], ["A274B3", 1.0]]

def validKey(key):
    # three or more characters, letters and digits only
    if len(key) < MIN_KEY_LENGTH or not key.isalnum():
        return False
    # the check digit is the right-hand digit of the sum of the other digits
    total = 0
    for character in key[0:len(key) - 1]:
        if character.isdigit():
            total = total + int(character)
    return str(total % 10) == key[len(key) - 1]

def validSignal(signal):
    return signal >= MIN_SIGNAL and signal <= MAX_SIGNAL

bad = 0
for record in signalTable:
    key = record[0]
    signal = record[1]
    if not validKey(key):
        print("Invalid key:", key, signal)
        bad = bad + 1
    elif not validSignal(signal):
        print("Invalid signal:", key, signal)
        bad = bad + 1
print(bad, "invalid records")
if bad == 0:
    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 a check digit?

An extra digit on the end of a code, worked out from the other digits, so that a mistyped code can be spotted. Here it is the last digit of the sum of the key's other digits.

How do I get the last character of a string in Python?

key[len(key) - 1], or key[-1]. The characters before it are key[0:len(key) - 1] or key[:-1].

What does isalnum() do?

It returns True if every character in the string is a letter or a digit, and False if there is a space, a symbol or nothing at all.

More from this paper

Every Edexcel 1CP2 question we have worked

Learn it step by step

  1. F6.1 Defensive design and validation Robust programs
  2. F4.1 Writing functions Functions and structured code
  3. F3.5 Two-dimensional arrays Strings, lists and records
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.