27-authentication.py
python-programming / robust-programming  ·  Lesson 28
Lesson 28 — Robust programming

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.

authentication-predict.py
stored_user = "student"
stored_password = "python"
username = "student"
password = "python"
print(username == stored_user and password == stored_password)
Predict before running
  • Predict the Boolean result.
  • Why is and used?
  • What happens if only the username matches?

Investigate

Use this code to explain exactly how the program works.

authentication-investigate.py
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)
Investigate the code
  • What two conditions keep the loop running?
  • What is the maximum number of attempts?
  • Which assignment records a successful login?

A simple authentication check

login.py
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")
How it works
  • 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.
AuthenticationAuthentication confirms identity. It is different from validation, which checks whether input follows acceptable rules.

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 focusUnderstand authentication as identity confirmation and be able to design simple username/password authentication in a high-level language.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
Authenticationcredential comparison in PythonConfirms identity.
username + passwordtwo input valuesBoth may need to match.
limited attemptscounter + condition-controlled loopA defensive rule can limit repeated guesses.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

Both credentials must match on the same attempt.

lesson-28-worked.py
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")
Output
Access granted
Work through it line by line
  • The stored credential values are fixed for this classroom example.
  • The user supplies two pieces of data.
  • and means the username and password must both match.
  • A mismatch on either value leads to denial.

Another example

An attempt counter can stop unlimited retries.

lesson-28-example-2.py
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")
Output
Depends on the entered passwords
Why this example matters
  • The loop stops for one of two reasons: correct password or three attempts.
  • The final selection reports which stopping condition occurred.

Common mistakes

Avoid these errors
  • 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-safe warningThese are simple programming examples. Real systems should not store plain-text passwords directly in source code.

Exam-ready checklist

You should now be able to
  • 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.