Community resourceWorksheet

J277 2.3.1 Programming validation and authentication

Part 3 of 7 · J277 2.3 · Producing robust programs

Defensive design written as runnable Python, using a loop that keeps asking until the input is acceptable.

Students will:

  • write the four parts of a reliable validation loop
  • express the invalid condition rather than the valid one
  • validate a number against a stated range
  • validate an entry against a list of accepted values
  • write a simple authentication check

Inside: 8 explanation cells, 4 runnable Python tasks, 1 multiple-choice question and 2 written answers. 17 marks, about 45 minutes.

Series: J277 2.3 · Producing robust programs, part 3 of 7.

Shared by Coding PathwayVerified teacher

  • 15 cells
  • About 45 minutes
  • CC BY-SA 4.0
  • Shared 17 Aug 2026
  • Updated 9 Sept 2026

Preview

The whole resource, exactly as a class sees it. Answers and marking are held back.

Programming validation and authentication

This worksheet turns defensive design into runnable Python. You will use selection and condition-controlled iteration to reject unsuitable input, request a replacement and continue only when the input is acceptable.

A reliable validation loop has four parts:

  1. receive an initial value;
  2. repeat while the value is invalid;
  3. explain the rule and receive a replacement inside the loop;
  4. continue with the accepted value after the loop.

The invalid condition is often easier to write than the valid condition. If an age must be from 11 to 18 inclusive, it is invalid when age < 11 or age > 18.

The numeric examples in this worksheet assume that the user enters text that Python can convert to a whole number with int(). If the user entered hello, int() would cause an error before the range check. OCR does not require exception handling here, so these tasks focus on writing the validation logic after the type has been accepted.

The validation-loop pattern

Control flow of an input-validation loop The program receives an input and checks whether it is invalid. If true, it displays a helpful message, requests a replacement and checks again. If false, it continues using the accepted value. Repeat only while the value is invalid Receive an input value Is the value invalid? Display the rule and receive a replacement value Continue using the accepted value True False

The True path loops back because the value is still unsuitable. The False path leaves the loop because the current value satisfies the rule.

Worked example: range validation

Predict which entries are rejected and which value is finally printed when the supplied inputs are 9, then 14.

Worked example
age = int(input("Enter age from 11 to 18: "))
while age < 11 or age > 18:
    print("Age must be from 11 to 18")
    age = int(input("Enter age from 11 to 18: "))
print("Accepted:", age)
Multiple choice1 mark

Why does the range-validation condition use or between age < 11 and age > 18?

  • AEither invalid boundary should make the loop repeat
  • BBoth invalid boundaries can be true at the same time
  • CThe age must be below 11 and above 18 together
  • DPython requires `or` in every while loop

Supported practice: validate a score

Complete the program so it keeps asking while score is below 0 or above 100. When an acceptable value is entered, display Accepted: followed by the score.

The checks will begin with a value below the range and a value above the range. Both boundary values, 0 and 100, must be accepted.

Coding task4 marks
score = int(input("Enter score from 0 to 100: "))

# Keep asking while score is invalid.

print("Accepted:", score)

Lookup-style validation

Sometimes a value must be one of a small accepted set. A direction in a game might be either left or right. The invalid condition is true only while the value matches neither accepted choice:

while direction != "left" and direction != "right":

The condition uses and because the value must be different from both accepted choices before it is rejected.

Independent practice: validate a direction

Complete the program so it keeps asking until direction is exactly left or right. Store the accepted text in direction and display it.

Do not use or between the two not-equal comparisons. That would remain true for every possible direction because one of the comparisons would always be true.

Coding task4 marks
direction = input("Enter left or right: ")

# Keep asking while direction is neither accepted value.

print("Moving", direction)

Simple authentication in practice

Authentication compares identifying information supplied by a user with stored information. In this GCSE example, a username and password must both match. Real systems need additional security controls, but the assessed programming principle here is the combined comparison.

Complete the next program using selection. Set authenticated to True only when both entered details match.

Coding task3 marks
stored_username = "studio_admin"
stored_password = "Mixer8"
username = input("Username: ")
password = input("Password: ")
authenticated = False

# Check both entered values.

print(authenticated)
Written answer2 marks

Explain why the score-validation program asks for another value inside the loop rather than only displaying an error message.

Link the replacement input to the loop condition eventually becoming false.

Students type their answer here.

Written answer3 marks

A student claims that validation and authentication are the same because both use if or while. Correct the claim.

Explain the different purpose of each process. The programming construct used is not its purpose.

Students type their answer here.

Review

Validation code should state the acceptance rule clearly, reject every unsuitable case, request corrected input and preserve the accepted value. Authentication checks identity information and should require all specified details to match.

For exam coding, test the value immediately below a boundary, the boundary itself and the value immediately above it. This quickly exposes incorrect <, >, <= or >= operators.