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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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
- Write programs that accept and respond appropriately to user input, and that read from and write to comma separated value text files.
A focused answer to Edexcel GCSE Computer Science 6.4.1 and 6.4.2, covering accepting and responding to user input, and reading from and writing to comma separated value (CSV) text files in Python.
- Identify the structural components of programs (constants, variables, initialisation, assignment, sequence, selection, repetition, iteration, data structures, subprograms, parameters, input/output) and write programs that use sequencing, selection, repetition (count-controlled, condition-controlled) and iteration with single entry and exit points.
A focused answer to Edexcel GCSE Computer Science 6.2, covering the structural components of programs and writing Python programs that use sequence, selection, count-controlled and condition-controlled repetition, and iteration.
- Write programs that manipulate strings (length, position, substrings, case conversion).
A focused answer to Edexcel GCSE Computer Science 6.3.3, covering string manipulation in Python: finding length, accessing characters by position, extracting substrings, and converting case.
- Write programs that use pre-existing and user-devised subprograms (procedures, functions), write functions that return values and procedures that do not, with or without parameters, use the arithmetic, relational and logical operators, and use global and local variables appropriately.
A focused answer to Edexcel GCSE Computer Science 6.5 and 6.6, covering functions and procedures, parameters and return values, built-in and user-defined subprograms, the operators, and global versus local variables in Python.
- Understand the threat to digital systems posed by malware (viruses, worms, Trojans, ransomware, key loggers), how hackers exploit technical vulnerabilities and use social engineering, and methods of protecting digital systems and data (anti-malware, encryption, acceptable use policies, backup and recovery).
A focused answer to Edexcel GCSE Computer Science 5.3.1 and 5.3.2, covering malware (viruses, worms, Trojans, ransomware, key loggers), how hackers exploit vulnerabilities and use social engineering, and protection methods (anti-malware, encryption, acceptable use policies, backup and recovery).
Sources & how we know this
- Pearson Edexcel GCSE (9-1) Computer Science (1CP2) specification — Pearson (2020)
- Pearson Edexcel GCSE Computer Science Programming Language Subset (Python) — Pearson (2020)