Home / GCSE · AQA / Computer Science / Programming

CS10Validation, Authentication, Testing and Errors

3.2.11 Data validation

Why programs validate input

Users make mistakes, so a good program checks input before it uses it. AQA: a validation routine is “a routine that contains code to check the validity of data”.

  • AQA: “Validity of data refers to data being reasonable/plausible/acceptable.” This might involve range checks, length checks and presence checks.
  • Validation makes a program robust: bad input is less likely to make it crash or give wrong output.
  • Validation checks data is sensible, not that it is correct. A shoe size of 9 typed as 8 passes a range check of 1 to 15, because 8 is a sensible shoe size. A shoe size of 80 would be rejected.
  • Verification is a different idea that isn’t named in the AQA course: checking data was typed or copied accurately, for example typing a new password twice. In a program, “do the two entries match?” is just another condition you can code.
  • In an exam, “how could this program be made more robust?” wants validation of the input. Comments and better variable names make code easier to read, not more robust.
Exam tipThe purpose of validation is to check that input is reasonable or acceptable before the program uses it. Fewer than half of students got this right in 2025: many wrote about the data being “correct”, which validation can’t check.
Validation routine
A routine that contains code to check the validity of data (that it is reasonable, plausible or acceptable).
Robust program
A program that copes with bad input without crashing or giving wrong results, for example because it validates input.
Verification
Checking data has been entered or copied accurately, such as typing it twice. Not named in the AQA course.
3.2.11 Data validation

Length, presence and range checks

AQA’s simple validation checks: a string has a minimum length, a string isn’t empty, and a number lies within a range (for example between 1 and 10).

  • Valid v invalid conditions. A month is valid when month >= 1 and month <= 12. It is invalid when month < 1 or month > 12. Swapping to the opposite condition flips and to or and each comparison to its opposite.
  • Impossible conditions. month < 1 and month > 12 can never be True (no number is both), so it would never reject anything.
  • Never write chained comparisons such as 1 <= month <= 12. They work in Python but not in AQA’s other languages, and examiners discourage them. Write two comparisons joined by and.
  • Inclusive means the limits themselves are allowed: “1 to 12 inclusive” accepts 1 and 12. Use >= and <= for the valid condition.
  • Type checks aren’t needed. AQA says exam questions assume input is the right type. You still convert it: int(input()) for a whole number, float(input()) for a real number.
  • A check can combine two rules: a nickname that must be entered and be at most 12 characters is invalid when nickname == "" or len(nickname) > 12.
CheckWhat it checksPython: data is invalid when…AQA pseudo-code
Length (minimum)A string has at least a certain number of characterslen(password) < 8LEN(password) < 8
Length (maximum)A string isn’t too longlen(nickname) > 12LEN(nickname) > 12
PresenceA string isn’t empty (something was entered)town == ""town = ''
RangeA number is between two limitsmonth < 1 or month > 12month < 1 OR month > 12
Worked exampleA quiz score must be a whole number from 0 to 40 inclusive. Write the Python condition that is True when the score is invalid.
  1. Valid: score >= 0 and score <= 40both limits are allowed
  2. Invalid: score < 0 or score > 40too low OR too high: flip each comparison and swap and for or
score < 0 or score > 40
Exam tipRead limits carefully. “From 5 to 30” includes 5 and 30. “More than 5” doesn’t include 5. Mixing up < and <= is the commonest slip in validation code.
Length check
Checks a string has a minimum (or maximum) number of characters.
Presence check
Checks a string isn’t empty: something has been entered.
Range check
Checks a number lies between two limits.
3.2.11 Data validation

Writing a validation loop

Validation uses indefinite iteration: keep asking until the input is valid. The loop must contain a fresh input, or it will never end.

  • Pattern 1: WHILE (works in Python). Input once before the loop. Loop while the data is invalid: output a message, then input again inside the loop.
    players = int(input("Number of players (2 to 4): "))
    while players < 2 or players > 4:
        print("Must be 2 to 4 players")
        players = int(input("Number of players (2 to 4): "))
    print(f"Starting a game for {players} players")
  • Pattern 2: REPEAT…UNTIL (AQA pseudo-code). The condition is at the end, so the input always happens at least once. It loops until the data is valid.
    REPEAT
      players ← STRING_TO_INT(USERINPUT)
      IF players < 2 OR players > 4 THEN
        OUTPUT 'Must be 2 to 4 players'
      ENDIF
    UNTIL players ≥ 2 AND players ≤ 4
  • Python has no REPEAT…UNTIL. Use pattern 1, or while True: with break when the input is valid (AQA accepts both).
    while True:
        players = int(input("Number of players (2 to 4): "))
        if players >= 2 and players <= 4:
            break
        print("Must be 2 to 4 players")
  • A Boolean flag avoids writing the condition twice: set valid = False, loop while valid == False, and set valid = True when every check passes.
  • Validation in a subroutine. Because programs validate many inputs, a function with the limits as parameters can be reused:
    def getNumber(prompt, low, high):
        number = int(input(prompt))
        while number < low or number > high:
            print(f"Enter a number from {low} to {high}")
            number = int(input(prompt))
        return number
    
    lives = getNumber("Lives: ", 1, 5)
    level = getNumber("Start level: ", 1, 20)

A validation loop: input, check, and ask again until the data is valid

Worked exampleWrite Python that asks for a team name and repeats until the name is at least 3 characters long.
  1. team = input("Team name: ")input once before the loop
  2. while len(team) < 3:loop while the data is invalid
  3. print("Too short")a message is good practice
  4. team = input("Team name: ")input again inside the loop, or it never ends
A WHILE loop that re-inputs the name while len(team) < 3.
Exam tipIf the question says “repeat until” or “keep asking”, you need a loop. Using if instead checks the input only once, and missing iteration is the biggest single mark-loser in AQA’s code questions.
Validation loop
An indefinite loop that keeps asking for input until it passes the checks.
Boolean flag
A True/False variable that records whether a condition has been met, such as valid.
3.2.11 Authentication

Authentication routines

AQA: an authentication routine is “a routine that contains code that verifies an identity. This might involve the entry of a username and password.” You only need plain text usernames and passwords: no encryption.

  • One stored pair. Keep asking while the username or the password is wrong. One wrong value is enough to refuse.
    username = input("Username: ")
    password = input("Password: ")
    while username != "kfox7" or password != "Lantern22":
        print("Login failed")
        username = input("Username: ")
        password = input("Password: ")
    print("Welcome")
  • Several users. Store usernames and passwords in two lists with matching positions, then check each position with a loop and a flag.
    users = ["kfox7", "tjin2", "amir9"]
    passwords = ["Lantern22", "Pebble8", "Orbit40"]
    username = input("Username: ")
    password = input("Password: ")
    found = False
    for i in range(len(users)):
        if users[i] == username and passwords[i] == password:
            found = True
    if found == True:
        print("Welcome")
    else:
        print("Login failed")
  • Limiting attempts. Count the attempts and stop the loop when the login succeeds or the limit is reached. After the loop, check which one ended it.
    attempts = 1
    password = input("Password: ")
    while password != "Lantern22" and attempts < 3:
        print("Wrong password")
        password = input("Password: ")
        attempts = attempts + 1
    if password == "Lantern22":
        print("Welcome")
    else:
        print("Account locked")
  • Each comparison needs its own variable: username == "kfox7" or username == "tjin2", not username == "kfox7" or "tjin2".
  • Authentication as a network security method (passwords, biometrics, CAPTCHA) is in the network security topic. Here you only write the code.

Authentication with a limit of three attempts

Exam tipCheck both values together. username != … and password != … lets someone in with the right username and a wrong password, because the loop stops as soon as one of them matches.
Authentication routine
A routine that contains code that verifies an identity, for example by checking a username and password.
Plain text
Stored as ordinary readable text. At GCSE, usernames and passwords in your code don’t need to be encrypted.
3.2.11 Testing

Testing and test plans

AQA: “Testing is a process to help the programmer to remove bugs from software and to ensure that it works correctly.”

  • Testing checks that a program does what it should with every kind of input, including input it should reject.
  • Test every path: an IF with an ELSE needs at least one test that goes each way.
  • A test plan is a table, written before testing, of the tests to carry out.
  • Fill in the expected result before running the program. Then run it and record the actual result. If they differ, there is a bug to find.
  • You can’t test every possible input, so choose test data carefully (next section).
TestPurposeTest dataExpected resultActual result
1Typical value is accepted6AcceptedAccepted
2Lowest allowed value is accepted1AcceptedRejected: bug
3Value just above the range is rejected13Error message, asked againError message, asked again
Exam tipIn a test plan question, the expected result must match what the program in the question does (for example “‘Try again’ is output”), not just “it works”.
Testing
A process to help the programmer to remove bugs from software and to ensure that it works correctly.
Test plan
A table of planned tests: test data, purpose, expected result and (after running) actual result.
Bug
An error in a program that makes it fail or behave wrongly.
3.2.11 Test data

Normal, boundary and erroneous test data

AQA names three types of test data: normal (typical), boundary (extreme) and erroneous.

  • AQA’s own example: if the allowed range is 1 to 10, then boundary data is 0, 1, 10, 11, “either side of the allowed boundary”.
  • So boundary data includes values that should be rejected (0 and 11) as well as values that should be accepted (1 and 10).
  • Boundary data matters because programmers often make mistakes at the limits, such as writing > for >=. A test of 1 catches that; a test of 5 doesn’t.
  • Length checks have boundaries too. For “at least 8 characters”, test a 7-character and an 8-character string.
  • Erroneous data is anything the program should refuse: out of range, an empty string for a presence check, a string that is too short.
TypeWhat it isAllowed range 1 to 10
Normal (typical)Sensible data the program should accept4, 7
Boundary (extreme)Values at the limits and just either side of them0, 1, 10, 11
ErroneousData that is wrong and should be rejected−5, 25

Test data for an allowed range of 1 to 10

Exam tipUse the limits in the question. For a range of 5 to 50, the boundary values are 4, 5, 50 and 51, not 0, 1, 10 and 11.
Normal (typical) data
Sensible data that the program should accept and process.
Boundary (extreme) data
Data at the limits of what is allowed and just either side of them, e.g. 0, 1, 10, 11 for 1 to 10.
Erroneous data
Data that is wrong (invalid) and should be rejected.
3.2.11 Test data

Choosing and justifying test data

You may be asked to choose test data for a given problem and say why. Work from the rule in the question.

  • 1. Find the rule. What are the limits? Are they included (“inclusive”, “at least”, “up to”)?
  • 2. Normal: a value comfortably inside the range.
  • 3. Boundary: each limit and the value just outside it.
  • 4. Erroneous: a value clearly outside the range.
  • 5. Expected result: say exactly what should happen: accepted, or rejected with the program’s message.
  • Justify by naming what the test proves: “14 is the highest value allowed, so it checks the program accepts the upper limit and hasn’t used < instead of <=.”
Worked exampleA hotel booking program accepts a number of nights from 1 to 14 inclusive. Choose test data and expected results for a test plan.
  1. Normal: 5 → accepteda typical stay inside the range
  2. Boundary: 1 → accepted; 14 → acceptedthe lowest and highest allowed values
  3. Boundary: 0 → rejected; 15 → rejectedjust either side of the limits
  4. Erroneous: 40 → rejectedclearly outside the range
5; 0, 1, 14, 15; 40 (with the expected result for each)
Worked exampleA password must be at least 10 characters. Give two boundary tests.
  1. A 9-character password → rejectedone below the minimum
  2. A 10-character password → acceptedexactly the minimum
Passwords of 9 and 10 characters
Exam tipAQA’s 2025 report: most students knew what boundary data is, but many then gave values that ignored the limits in the question. Always build the test data from the question’s own numbers.
3.2.11 Types of error

Syntax errors and logic errors

AQA names two types of error. Know their definitions and be able to spot each in code.

  • A syntax error is about the form of the code: it breaks the language’s rules, so it can’t be run.
  • A logic error is about the meaning: the code is legal and runs, but does the wrong thing.
  • A loop that never ends because a variable is never updated is a logic error: the code runs, but wrongly.
  • AQA only asks about these two types.
Syntax errorLogic error
AQA’s definitionCode does not follow the rules of the language grammar. It will prevent code from being executed.A mistake in code that causes it to produce unexpected or incorrect responses. It does not prevent a program from running.
Found byThe translator (compiler or interpreter), which reports itTesting and tracing: the program runs, but the results are wrong
Python exampleswhlie for while; a missing bracket print(total; if x = 5: (one = in a condition)> where >= was needed; range(1, 10) when 10 should be included; dividing by the wrong number; and for or
Exam tipWhen asked to describe the errors in a given line, describe that line’s errors (“the closing bracket is missing”), not error types in general.
Syntax error
When code does not follow the rules of the language grammar. It prevents the code from being executed.
Logic (logical) error
A mistake in code that causes it to produce unexpected or incorrect responses. It does not prevent the program from running.
3.2.11 Correcting errors

Finding and correcting errors

You must be able to correct errors in algorithms and programs, in pseudo-code or in Python.

  • Find it: trace the code with a test value (see the tracing topic), especially a boundary value. The first row where the trace differs from what should happen points at the faulty line.
  • Common logic errors: < v <=; a loop that stops one too early (range(1, 10) for 1 to 10); a total or counter that isn’t reset or isn’t increased; and v or; the wrong variable in a calculation.
  • Fix it in full: write the corrected line, not just the changed symbol, and make sure the fix keeps the rest of the program working.
  • Change both parts if needed: turning “stop when below 0” into “stop when 0 or more” means < becomes >=, not >.
Worked exampleThis program should output the total of 5 prices, then the mean price. Find and correct the logic error.
total = 0
for i in range(5):
    price = float(input("Price: "))
    total = total + price
mean = total / 4
print(f"Total £{total}, mean £{mean}")
  1. Test with 2, 2, 2, 2, 2: total 10, so the mean should be 2pick values that make the right answer obvious
  2. The program outputs a mean of 2.5the actual result differs from the expected result
  3. Line 5 divides by 4, but there are 5 pricesthe loop runs for i = 0 to 4: five times
mean = total / 5
Exam tipWhen a question has one syntax error and one logic error, look for both. In 2022 most students fixed the syntax error and missed the logic error.