Edexcel GCSE Computer Science June 2023 Paper 2, Question 5: the user ID generator
Pearson Edexcel 1CP2/02 June 2023, Question 5: amend a Python program that builds a user ID such as bassirv403 from a name and a date of birth. Local and global names, a welcome procedure, lower(), isdigit() validation and ord() in place of int(). With the program to run.
Question 5 of the Pearson Edexcel GCSE Computer Science Paper 2 sat on 25 May 2023 (1CP2/02) is a 15 mark question where you improve a program that half works. It touches five topics at once: local and global variables, procedures, string methods, validation and ASCII codes.
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
A user ID is the last name, then the first letter of the first name, then a number, all in lower case. The number is the sum of the ASCII values of the eight digits of the date of birth, written ddmmyyyy. Viola Bassir, born 15 June 2005, gets bassirv403.
You are asked to:
- rename the parameters of
makeID()so that they are not confused with global variables of the same name; - add a procedure with no parameters that welcomes the user, and call it before any input;
- convert the two names to lower case;
- validate the date of birth in the main program with a built-in string method, so that only the digits 0 to 9 are allowed. Call
makeID()only if it is valid, and tell the user if it is not; - fix the logic error in
makeID(): it usesint()on each digit where it needs the ASCII value.
Check the number by hand
The ASCII code of "0" is 48, and the digits follow on: "1" is 49, "5" is 53. For 15062005: 49 + 53 + 48 + 54 + 50 + 48 + 48 + 53 = 403. With int() the sum would be 1 + 5 + 0 + 6 + 2 + 0 + 0 + 5 = 19. The function that gives an ASCII value is ord().
A finished program
def makeID(pLast, pFirst, pBirth):
letterPart = pLast + pFirst[0]
numberPart = 0
for character in pBirth:
numberPart = numberPart + ord(character)
return letterPart + str(numberPart)
def welcome():
print("Welcome to the ID maker")
welcome()
lastName = input("Last name: ")
firstName = input("First name: ")
birthDate = input("Date of birth (ddmmyyyy): ")
lastName = lastName.lower()
firstName = firstName.lower()
if birthDate.isdigit():
myID = makeID(lastName, firstName, birthDate)
print(myID)
else:
print("Invalid date of birth")
Where the fifteen marks are
Nine single marks: one parameter renamed; all three renamed; the new names meaningful; int() replaced with ord(); the welcome subprogram defined with def, a name and brackets; a welcome message that fits; the welcome called before any input; a name converted with .lower(); and the digits validated.
Then up to 3 for solution design, where isdigit() is preferred to a loop over the characters, and up to 3 for how well it runs.
Where the marks are lost
- Renaming the parameters and not the lines that use them. Every use inside
makeID()has to change too, or the function quietly goes on using the globals. .lowerwith no brackets. It is a method call:lastName.lower(). And the result must be stored.lastName.lower()on a line of its own changes nothing.- A
returnin the welcome procedure, or parameters in its brackets. A procedure with no parameters has empty brackets and returns nothing. - Calling
makeID()before checking the date. Invalid input should not be processed. chr()forord().chrturns a code into a character.ordturns a character into its code.
Run it
Try the test data from the mark scheme.
The program
from bugbot import *
connect()
def makeID(pLast, pFirst, pBirth):
letterPart = pLast + pFirst[0]
numberPart = 0
for character in pBirth:
numberPart = numberPart + ord(character)
return letterPart + str(numberPart)
def welcome():
print("Welcome to the ID maker")
welcome()
lastName = input("Last name: ")
firstName = input("First name: ")
birthDate = input("Date of birth (ddmmyyyy): ")
lastName = lastName.lower()
firstName = firstName.lower()
if birthDate.isdigit():
myID = makeID(lastName, firstName, birthDate)
led("green")
print(myID)
else:
led("red")
print("Invalid date of birth")
Questions
What does ord() do in Python?
It returns the ASCII (Unicode) code of a character, so ord("0") is 48 and ord("A") is 65. chr() does the opposite.
What is the difference between a local and a global variable?
A global variable is made in the main program and can be seen everywhere. A local variable, including a parameter, exists only inside its subprogram. Giving them different names stops the two being confused.
How do I check that a string contains only digits?
Use the string method isdigit(): birthDate.isdigit() is True only if every character is a digit from 0 to 9.
More from this paper
- Question 2: Fix syntax, name, index and logic errors in a program that picks random exercises 8 marks
- Question 4: The volume of a prism: validation, rounding and format() in 8 columns 15 marks
- Question 6: Authenticate against a 2D array with three outcomes 15 marks
Every Edexcel 1CP2 question we have worked
Learn it step by step
- F4.3 Local and global variables Functions and structured code
- F3.2 Character codes and conversion Strings, lists and records
- F4.1 Writing functions Functions and structured code
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.