Authentication
Confirm a user’s identity with a simple username and password check.
Authentication checks that a user is who they claim to be. A simple classroom example compares entered credentials with stored values.
Predict
Read the code first. Do not run it yet.
stored_user = "student"
stored_password = "python"
username = "student"
password = "python"
print(username == stored_user and password == stored_password)- Predict the Boolean result.
- Why is
andused? - What happens if only the username matches?
Investigate
Use this code to explain exactly how the program works.
correct_password = "code123"
attempts = 0
authenticated = False
while attempts < 3 and authenticated == False:
password = input("Password: ")
attempts = attempts + 1
if password == correct_password:
authenticated = True
print(authenticated)- What two conditions keep the loop running?
- What is the maximum number of attempts?
- Which assignment records a successful login?
A simple authentication check
STORED_USERNAME = "student"
STORED_PASSWORD = "python"
username = input("Username: ")
password = input("Password: ")
if username == STORED_USERNAME and password == STORED_PASSWORD:
print("Access granted")
else:
print("Access denied")- Both comparisons must be True for access to be granted.
- The stored credentials are represented as constants for this simple example.
- Real systems require stronger password storage and security; this example is only for learning the programming logic.
Self-study guide
Authentication confirms the identity of a user. OCR expects practical experience of simple authentication such as a username and password. In a classroom program, this normally means comparing entered credentials with stored values and deciding whether access is allowed.
A real authentication system would store credentials securely rather than keeping a plain-text password inside source code. The simple examples here exist to teach the programming logic, not real-world credential storage.
OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
Authentication | credential comparison in Python | Confirms identity. |
username + password | two input values | Both may need to match. |
limited attempts | counter + condition-controlled loop | A defensive rule can limit repeated guesses. |
Worked example
Both credentials must match on the same attempt.
STORED_USER = "nova"
STORED_PASSWORD = "circuit7"
username = input("Username: ")
password = input("Password: ")
if username == STORED_USER and password == STORED_PASSWORD:
print("Access granted")
else:
print("Access denied")Access granted
- The stored credential values are fixed for this classroom example.
- The user supplies two pieces of data.
andmeans the username and password must both match.- A mismatch on either value leads to denial.
Another example
An attempt counter can stop unlimited retries.
PASSWORD = "orbit"
attempts = 0
password = ""
while password != PASSWORD and attempts < 3:
password = input("Password: ")
attempts = attempts + 1
if password == PASSWORD:
print("Access granted")
else:
print("Locked")Depends on the entered passwords
- The loop stops for one of two reasons: correct password or three attempts.
- The final selection reports which stopping condition occurred.
Common mistakes
- Describing validation as authentication. Validation checks data rules; authentication checks identity.
- Checking only the password when the scenario requires both username and password.
- Forgetting to update the attempts counter.
- Presenting hard-coded plain-text passwords as good real-world security practice.
Exam-ready checklist
- Define authentication accurately.
- Build simple username/password checks.
- Combine credentials with Boolean logic.
- Use a limited-attempt loop when required.
- Distinguish authentication from input validation.
What you have learned
- Authentication confirms a user’s identity.
- Simple authentication can compare a username and password.
- Both credentials can be combined with
and. - A loop can limit repeated attempts.