GCSE · Computer Science · Eduqas · Spec C500QS

Authentication management routines

Three wrong passwords and the app slams the door. But what if the third one is right? Run that decision line by line, then break it.

Be the program

Run the login, one line at a time

Three different people type into the same routine. Call every counter, flag and message before it appears.

Be the attacker and the program at once: which line stops each attempt?

MAX_ATTEMPTS = 3
users = {"mia": "kiwi42", "sam": "tiger7"}
attempts = 0
logged_in = False
while not logged_in and attempts < MAX_ATTEMPTS:
username = input("Username: ")
password = input("Password: ")
if username in users and password == users[username]:
logged_in = True
print("Access granted")
else:
attempts = attempts + 1
print("Incorrect username or password")
if not logged_in:
print("Access denied. Too many tries.")
attemptslogged_inusernamepasswordloop again?
0

Step 1. Lines 0 and 1 have already run: MAX_ATTEMPTS is 3 and users is set up, so the table starts here.

Step 1 of 8Called right: 0 of 3

Only the print() lines count as output here, not the Username: and Password: prompts. users is a dictionary: mia maps to kiwi42 and sam maps to tiger7.

Now you write it

Build a login for ali and bea

Build a login routine for ali and bea, one chunk at a time. When a gap appears, choose the line that belongs there. A wrong pick tells you what that line would really do.

  1. MAX_ATTEMPTS = 3 users = {"ali": "mango9", "bea": "plum12"} attempts = 0 logged_in = FalseThe limit lives in one named constant, MAX_ATTEMPTS, so changing the number of tries means editing one value. attempts counts failures. logged_in is the flag.
  2. missing step
Which line is step 2?

Exam line: Writing it yourself: while not logged_in and attempts < limit, an if with and, the counter inside else, and if not logged_in after the loop.

Spot the hole

One word lets a stranger in

A classmate knows only that a user called "ali" exists. They type ali and any password they like, and they walk straight in. Which line lets them?

A student's routine — which line goes wrong?

Exam line: Check the joining word: and needs both comparisons to be True, or needs only one.

Change it, run it

Four new requirements for the routine you traced

This routine is the one you traced. It needs four changes, one at a time: (1) a username that isn't stored must be refused; (2) the user gets five attempts, not three; (3) after a failed login the user is told how many attempts remain; (4) a third user, ali, with password mango9, must be able to log in. Press Run, read what happens, then pick the fix for the highlighted line.

Each run types the inputs you can see echoed in its own output.

Python 3
  1. MAX_ATTEMPTS = 3
  2. users = {"mia": "kiwi42", "sam": "tiger7"}
  3. attempts = 0
  4. logged_in = False
  5. while not logged_in and attempts < MAX_ATTEMPTS:
  6. username = input("Username: ")
  7. password = input("Password: ")
  8. if password == users[username]:
  9. logged_in = True
  10. print("Access granted")
  11. else:
  12. attempts = attempts + 1
  13. print("Incorrect username or password")
  14. if not logged_in:
  15. print("Access denied. Too many tries.")

Every message and output below was produced by running that exact program in Python 3 with the inputs shown in the output. This page itself runs nothing.

Say why it matters

What does each part protect against?

A login routine has three parts: (1) the username and password comparison, (2) the attempt limit and lockout, (3) the same failure message whichever detail was wrong. Explain what each part protects against. [6 marks] [6 marks]

0 words · your answer stays on this page and is not sent anywhere.

Exam line: For each part give the threat, then the reason that part stops it.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

A login is a small loop where every line has a job. Run it, break it, then say what each part protects.

What you need to know

  • Authentication means confirming a user is who they claim to be before the program lets them in.
  • An authentication management routine is code that controls access, for example by comparing entered details with stored values.
  • Read both details with input(), then grant access in an if/else only when username AND password both match, joined with and.
  • A dictionary can map each username to its password, so the routine compares the entered password with that same user's stored one.
  • Have a goZara insists the routine only needs to check that the typed password is stored for somebody. Sam's friend types sam and kiwi42, which is Mia's password. Is Zara's routine safe?

    No. It would let the friend in as sam.

    The entered password has to match the one stored for that same username, and sam's is not kiwi42.

  • With ==, two strings must match exactly: different capital letters or extra spaces mean no match.
  • Have a goThe stored password is kiwi42. Someone types Kiwi42, and someone else types kiwi42 with a space on the end. Does either get in?

    Neither of them.

    == is exact, so a capital letter or an extra space makes a different string.

  • If the entered username isn't stored, the routine must refuse access rather than crash or compare against a missing value.
  • A counter variable records the number of failed attempts and is updated after each failed attempt.
  • A Boolean flag records success; a while loop repeats the prompt while the flag is False and attempts are below the limit.
  • Have a goThe limit is 3. Two logins have failed, so the counter holds 2 and the flag is still False. Does the prompt appear a third time?

    Yes.

    2 is still below 3 and the flag is False, so the loop condition holds; the lockout only starts once the counter reaches the limit.

  • Reaching the limit without a login locks the user out and displays an access denied message.
  • Each part guards against something different: the password check, the attempt limit, and the general failure message.

The big picture

An authentication management routine confirms a user is who they claim to be before the program lets them in. In Python 3 it compares the entered username and password with stored values using and, counts failed attempts, repeats the prompt with a while loop and locks the user out at the limit. Each part protects against something different.

Key points

1Access needs the right username AND that user's own password, matched exactly, joined with and.
2Check the username is stored before looking its password up, so an unknown name is refused instead of crashing the program.
3The counter rises after each failed attempt; the loop runs while the flag says not logged in and the counter is below the limit.
4Keep the limit in one constant such as MAX_ATTEMPTS = 3, so changing the number of attempts means editing one value.
5Password check: unauthorised access. Attempt limit and lockout: brute-force guessing. Same failure message: attackers learning which usernames exist.

Worked example

Problem

MAX_ATTEMPTS is 4 and a user types three wrong logins in a row. After each failure the routine prints how many attempts remain. What are the three numbers, and what would a fourth wrong login do?

⚠ Watch out

Thinking any stored password will do, or that a near-match such as Kiwi42 for kiwi42 is close enough. Access needs the exact username AND that same user's exact password.

🧠

Memory hook

Whose password? How many goes? What do you tell them? Get all three right and the door stays shut to attackers.

✓

Check yourself

Cover the page. Write the one if-line that grants access using users, then say what happens when the username typed is not in users and why the routine doesn't crash.

Flashcards

(14)
What is authentication?
Confirming that a user is who they claim to be before the program gives them access.
What is an authentication management routine?
A section of program code that controls a user's access, for example by comparing an entered username and password with stored values and limiting the number of attempts.
Which word joins the username and password comparisons in the login condition, and why?
and. Access is granted only when both match, so someone who knows or guesses only a username is refused. With or, a stored username alone would be enough.
Which stored password must the entered password match?
The one stored for that same username, not a password stored for any other user.
How does == treat capital letters and extra spaces when it compares two strings?
Exactly and case-sensitively, so an entered value with different capitals or extra spaces does not match the stored value.
How can the stored usernames and passwords be held, and how is a password found?
In a data structure such as a Python dictionary mapping each username to its password. The routine looks up the entered username to find the password to compare.
What must the routine do if the entered username is not among the stored usernames?
Refuse access, rather than crash or compare against a missing value.
What does the counter variable record, and when is it updated?
The number of failed attempts (or the attempts remaining), updated after each failed attempt.
When does a while loop repeat the login prompt?
While the user has not logged in and the number of attempts is below the limit.
What is the Boolean flag for?
It records whether the login has succeeded, so the loop stops as soon as the correct details are entered.
What happens when the attempt limit is reached with no successful login?
The routine refuses further attempts (locks the user out) and displays a message saying access is denied.
Why store the limit in one named constant such as MAX_ATTEMPTS = 3?
The routine can be changed to allow a different number of attempts by editing one value.
Which attack does limiting the number of attempts protect against?
A brute-force attack, in which an attacker repeatedly tries many possible passwords until one works.
Why show the same failure message whether the username or the password was wrong?
So an attacker cannot learn which usernames exist.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More Eduqas GCSE Computer Science topics

See the full Eduqas Computer Science curriculum →

How this lesson was checked. This Eduqas GCSE Computer Science (specification C500QS)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 16 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.