Chhetri AcademyGCSE & A level Paper Builder

6.1Developing code: readability, errors, validation and testing

Edexcel GCSE Computer Science (1CP2) · Problem solving with programming

Practise Developing code: readability, errors, validation and testing. 29 exam-style questions plus unlimited generated ones on this subtopic, at up to four difficulty levels, with full mark schemes and a progress tracker. Free, no account needed.

Build a paper on this topic

▶ Watch videos on Developing code: readability, errors, validation and testing (Craig 'n' Dave Edexcel on YouTube) · Practise all of Problem solving with programming

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

Students log in to an online homework website with a username and a password. State the purpose of authentication.
To check that a user is who they claim to be before they are allowed access.
State the type of error that stops a program from being translated and run.
Syntax error
State the type of validation check that your while loop carries out.
A range check.

Sample questions

Written for this site in the style of Edexcel exam questions. They are not taken from real past papers.

Question 1Easy6 marks
Programmers use several techniques to make their code easier to read, understand and maintain.
(a) Give three techniques that make program code easier to read.[3]
(b) Identify the change that would not make a program easier to read.[1]
  • Adding comments to explain each section of code
  • Using the identifier totalCost instead of tc
  • Writing as many statements as possible on each line
  • Leaving a blank line between sections of code
(c) A programmer needs to change a program that was written by someone else a year ago.
Explain one way in which comments in the code would help the programmer.[2]
Show the answer and mark scheme
(a) Answer: For example: comments, meaningful (descriptive) identifiers and indentation.
  • comments (to explain what the code does)
  • meaningful / descriptive identifiers (for variables and subprograms)
  • indentation (to show the structure of loops, selection and subprograms)
  • white space / blank lines between sections of code
  • a consistent layout, e.g. one statement on each line
(b) Answer: Writing as many statements as possible on each line
(c) Answer: Comments explain what each part of the code does, so the programmer can quickly find and understand the part that needs changing without working it out line by line.
  • comments explain the purpose of sections / lines of code
  • so the programmer can find / understand the code that needs changing more quickly / is less likely to introduce errors
Question 2Medium10 marks
This program works correctly, but it is difficult to read.
01 a=0
02 b=0
03 for c in range(5):
04   d=int(input("?"))
05   if d>=40:
06     a=a+1
07   b=b+d
08 print(a,b/5)
(a) Describe the purpose of the program.[2]
(b) Give two ways in which the program could be made easier to read. For each way, give an example from this program.[4]
(c) Amend the program so that it is easier to read. It must still work in exactly the same way.[4]
Show the answer and mark scheme
(a) Answer: It inputs five marks, counts how many of them are 40 or more, and outputs this count and the mean of the five marks.
  • counts how many of the five values entered are 40 or more (e.g. passes)
  • outputs this count and the mean / average of the five values
(b) Answer: Use meaningful identifiers, e.g. passes and total instead of a and b. Add comments, e.g. to explain that 40 is the pass mark.
  • use meaningful identifiers
  • e.g. passes / total / mark instead of a / b / d
  • add comments
  • e.g. to explain that the loop inputs five marks / that 40 is the pass mark
  • add white space
  • e.g. spaces around = and >= / a blank line before the print statement
  • use a named constant
  • e.g. NUMBER_OF_MARKS = 5 instead of writing 5 in two places
(c) Answer:
# Count the passes and find the mean of five test marks
passes = 0
total = 0
for count in range(5):
    mark = int(input("Enter a mark: "))
    # A mark of 40 or more is a pass
    if mark >= 40:
        passes = passes + 1
    total = total + mark

# Output the number of passes and the mean mark
print(passes, total / 5)
  • meaningful identifiers for all four variables
  • at least two sensible comments
  • white space, e.g. spaces around operators and blank lines between sections
  • the logic is unchanged: same loop, condition, calculations and output
Question 3Hard9 marks
A computer club stores its members' usernames and passwords in two lists. The password for each username is at the same index in the list passwords.
usernames = ["akhan", "bjones", "cwu"]
passwords = ["Tiger#81", "sunset22", "Qx7!pond"]
(a) Write a program that:
  • asks the user to enter a username and a password
  • checks whether the username is in the list and the password is the one stored at the same index
  • gives the user a maximum of three attempts
  • outputs Access granted as soon as correct details are entered, or Account locked after three failed attempts.
You should add to the two lines of code above.[7]
(b) Explain why the program should not tell the user whether it was the username or the password that was wrong.[2]
Show the answer and mark scheme
(a) Answer:
usernames = ["akhan", "bjones", "cwu"]
passwords = ["Tiger#81", "sunset22", "Qx7!pond"]
attempts = 0
granted = False
while attempts < 3 and not granted:
    user = input("Username: ")
    word = input("Password: ")
    for index in range(len(usernames)):
        if usernames[index] == user and passwords[index] == word:
            granted = True
    attempts = attempts + 1
if granted:
    print("Access granted")
else:
    print("Account locked")
  • a loop that allows at most three attempts (with the attempts counted)
  • the loop stops as soon as correct details are entered (e.g. a flag in the condition)
  • inputs a username and a password on each attempt
  • checks every username in the list
  • compares the password with the one at the same index as the matching username
  • access is only granted when both the username and the password match
  • outputs Access granted when the details are correct
  • outputs Account locked after three failed attempts (and not otherwise)
(b) Answer: It would tell someone trying to break in that the username exists, so they would only need to guess the password.
  • it would tell an attacker that the username exists / is valid
  • so they would only need to guess the password (making unauthorised access easier)

Related subtopics

Stuck? Get 1-to-1 help. Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.

Book a free trial