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 = 3users = {"mia": "kiwi42", "sam": "tiger7"}attempts = 0logged_in = Falsewhile not logged_in and attempts < MAX_ATTEMPTS:username = input("Username: ")password = input("Password: ")if username in users and password == users[username]:logged_in = Trueprint("Access granted")else:attempts = attempts + 1print("Incorrect username or password")if not logged_in:print("Access denied. Too many tries.")
| attempts | logged_in | username | password | loop 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.
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.
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.
- 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 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.")
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.
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
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?
What is an authentication management routine?
Which word joins the username and password comparisons in the login condition, and why?
Which stored password must the entered password match?
How does == treat capital letters and extra spaces when it compares two strings?
How can the stored usernames and passwords be held, and how is a password found?
What must the routine do if the entered username is not among the stored usernames?
What does the counter variable record, and when is it updated?
When does a while loop repeat the login prompt?
What is the Boolean flag for?
What happens when the attempt limit is reached with no successful login?
Why store the limit in one named constant such as MAX_ATTEMPTS = 3?
Which attack does limiting the number of attempts protect against?
Why show the same failure message whether the username or the password was wrong?
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 postedMore Eduqas GCSE Computer Science topics
- Abstraction
- Analyse a set of requirements
- Comparing approaches to requirements
- Consistent programming style
- Creating and extending Python 3 functions/methods
- Creating and extending Python 3 libraries
- Decomposition
- Designing a testing strategy
- Explaining test outcomes
- File handling: open a file
- File handling: read into a variable
- File handling: read into an array
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.