Skip to main content
EnglandComputer ScienceSyllabus dot point

How do you validate user input and authenticate users in a Python program?

Understand the need for and write programs that implement validation (length check, presence check, range check, pattern check) and authentication (ID and password, lookup).

A focused answer to Edexcel GCSE Computer Science 6.4.3 and 6.4.4, covering the need for validation, the length, presence, range and pattern checks, and authentication by ID and password lookup in Python.

Generated by Claude Opus 4.89 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. Why validation is needed
  3. The validation checks
  4. Validation uses a loop
  5. Authentication
  6. Try this

What this dot point is asking

Edexcel wants you to explain the need for validation, write programs that perform the named validation checks (length, presence, range, pattern), and implement authentication by checking an ID and password (including a lookup), in Python.

Why validation is needed

Programs cannot trust that users type sensible values, by accident or on purpose, so validation guards the input. Without it, a program might divide by a zero the user entered, store an empty name, or accept an age of 500. Validation catches these early and asks for correct input instead, which is central to writing robust software.

The validation checks

# Presence check
name = input("Name: ")
while name == "":
    name = input("Name cannot be blank. Name: ")

# Length check (at least 8 characters)
password = input("Password (min 8): ")
while len(password) < 8:
    password = input("Too short. Password (min 8): ")

# Range check (0 to 100 inclusive)
mark = int(input("Mark (0-100): "))
while mark < 0 or mark > 100:
    mark = int(input("Out of range. Mark (0-100): "))

Each check uses a loop that re-prompts until the input is valid, which is the standard pattern. A pattern check confirms the input fits a format, for example that a product code is a letter followed by three digits; at GCSE this can be done by checking the length and that the right positions are letters or digits.

Validation uses a loop

This re-prompting structure (read once, then loop while invalid) is what the exam expects, and it is the practical version of the REPEAT-UNTIL idea from algorithms: you must ask before you can check, so the input statement appears before and inside the loop.

Authentication

STORED_USER = "admin"
STORED_PASS = "pass123"
username = input("Username: ")
password = input("Password: ")
if username == STORED_USER and password == STORED_PASS:
    print("access granted")
else:
    print("access denied")

The key point is that both the ID and the password must match, so they are combined with AND. A lookup version checks the entered ID and password against a stored list or file of users (for example finding the username's record and comparing the password). Authentication is the programming side of the security topic: it is how a system controls who can log in.

Try this

Q1. State which validation check confirms a number is between a minimum and maximum. [1 mark]

  • Cue. A range check.

Q2. State what must match for authentication to grant access with an ID and password. [1 mark]

  • Cue. Both the ID (username) and the password must match the stored values.

Exam-style practice questions

Practice questions written in the style of Pearson Edexcel exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Edexcel 20225 marksWrite a Python program that keeps asking the user to enter a percentage until they enter a whole number between 0 and 100 inclusive (a range check), then prints the accepted value.
Show worked answer →

Use a condition-controlled loop that repeats until the value is in range. A REPEAT-UNTIL style is implemented in Python with a while loop.

value = int(input("Enter a percentage (0-100): "))
while value < 0 or value > 100:
    print("Out of range, try again.")
    value = int(input("Enter a percentage (0-100): "))
print("Accepted:", value)

Markers reward reading the input, a loop that repeats while the value is outside 0 to 100, a correct range condition (value < 0 or value > 100), re-prompting inside the loop, and outputting the accepted value. The boundaries 0 and 100 must be allowed (inclusive).

Edexcel 20215 marksA program stores a username and password. Write a Python program that asks for a username and password and prints 'access granted' only if both match the stored values 'admin' and 'pass123', otherwise prints 'access denied'.
Show worked answer →

Read both inputs and compare against the stored values with AND.

STORED_USER = "admin"
STORED_PASS = "pass123"
username = input("Username: ")
password = input("Password: ")
if username == STORED_USER and password == STORED_PASS:
    print("access granted")
else:
    print("access denied")

Markers reward reading both values, comparing each to the stored value, combining with AND so both must match, and the correct output for each case. Granting access if only one matches would be wrong.

Related dot points

Sources & how we know this