Edexcel GCSE Computer Science June 2023 Paper 2, Question 6: authentication with a 2D array

Pearson Edexcel 1CP2/02 June 2023, Question 6: write a Python program that rejects blank input, searches a 2D array of user names and passwords, and gives one of three messages: welcome, wrong password, or user not found. A model answer and the fifteen marks.

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

Question 6 is the last question of the Pearson Edexcel GCSE Computer Science Paper 2 sat on 25 May 2023 (1CP2/02): 15 marks for a program written from requirements. It is a linear search of a two-dimensional array with three possible outcomes, not two.

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

The question in short

User names and passwords are stored in a two-dimensional array. Each record is a name and a password.

  • Input: prompt for a user name and a password. Neither should be blank.
  • Process: search the array for the name and password. It must work for any length of array.
  • Output: a message for a correct name and password; a different message when the name is found but the password does not match; and another when the name is not found at all.

The idea

Search by name only. When the name is found, stop, and then look at the password in the same record. That gives the three outcomes, and it means each record is visited once at most.

A model answer

name = input("User name: ")
password = input("Password: ")

if name == "" or password == "":
    print("User name and password must not be blank")
else:
    foundName = False
    index = 0
    while index < len(userTable) and not foundName:
        if userTable[index][0] == name:
            foundName = True
        else:
            index = index + 1

    if not foundName:
        print("User name not found")
    elif userTable[index][1] == password:
        print("Welcome")
    else:
        print("Incorrect password")

Where the fifteen marks are

Six single marks: two string inputs; a check for blank input with a relational operator; a loop that goes through the table; a logical operator somewhere in a compound test; the fields of a record reached with [index][0] and [index][1]; and a way of telling the three states apart.

Then up to 3 each for solution design, good programming practice and functionality. The design marks look for a loop that stops when the name is found, so that records are not visited needlessly. Practice is meaningful names, white space and useful comments.

Where the marks are lost

  • Two outcomes only. "Access denied" for both failures misses a requirement. The question wants wrong password and unknown user told apart.
  • Printing inside the loop. "User name not found" would print once for every record that does not match. Decide after the loop.
  • range(5). Any length of array: len(userTable).
  • No blank check. It is two of the six single marks: the relational operator and, usually, the logical one.
  • No comments. They are marked on this question.

Run it

A small table of made-up users. The light is green for welcome, yellow for a wrong password and red for an unknown user.

Try ada with lovelace (Welcome), ada with turing (Incorrect password), bob with anything (User name not found), and a blank name.
The program
from bugbot import *
connect()

userTable = [["ada", "lovelace"], ["alan", "turing"], ["grace", "hopper"]]

name = input("User name: ")
password = input("Password: ")

if name == "" or password == "":
    print("User name and password must not be blank")
else:
    # search for the name only, and stop as soon as it is found
    foundName = False
    index = 0
    while index < len(userTable) and not foundName:
        if userTable[index][0] == name:
            foundName = True
        else:
            index = index + 1

    # three outcomes
    if not foundName:
        led("red")
        print("User name not found")
    elif userTable[index][1] == password:
        led("green")
        print("Welcome")
    else:
        led("yellow")
        print("Incorrect password")
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 GCSE Computer Science 2023 Paper 2 Question 6?

Input the name and password and reject blanks. Search the table with a while loop that stops when the name is found. After the loop: if the name was not found say so, else if the password in that record matches say welcome, else say the password is incorrect.

How do I search a two-dimensional array in Python?

Loop over the row index. Compare one field of each record, such as table[index][0], with the target, and stop when it matches. The other fields of the match are then table[index][1] and so on.

Why use a while loop for a search?

A while loop can stop as soon as the item is found, so no record is visited needlessly. A for loop with a break does the same. A plain for loop always runs to the end.

More from this paper

Every Edexcel 1CP2 question we have worked

Learn it step by step

  1. F6.2 Authentication Robust programs
  2. F3.5 Two-dimensional arrays Strings, lists and records
  3. F13.4 Programming questions Exam preparation
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.