Robust programs · GCSE · OCR J277 2.3.1, AQA 8525 3.2.11, Edexcel 1CP2 6.4.4 · about 15 min
Usernames and passwords, limiting attempts, and stronger ways to prove who someone is.
[1 mark]What is authentication?
[1 mark]What does this program print?
operators = {"ada": "robot42", "alan": "turing1"}
name, password = "ada", "turing1"
print(name in operators and operators[name] == password)False
turing1 is a real password, but it belongs to alan, not ada.
[1 mark]Why should a login say "access denied" rather than "no such user"?
[1 mark]Why limit the number of login attempts?
[1 mark]A password and a code sent to your phone is an example of what?
Use operators = {"ada": "robot42", "alan": "turing1"}. Ask Username? and Password? up to three times. For each wrong pair print access denied. When a pair is right, print welcome <name>, turn the LED green and drive 20 cm forward. The task types alan and wrong, then alan and turing1.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
operators = {"ada": "robot42", "alan": "turing1"}The hint students can ask for: Give the loop a fixed number of attempts. Each time, check the name exists and that the stored password matches it before letting them in, and stop asking once they are.
from bugbot import *
connect()
operators = {"ada": "robot42", "alan": "turing1"}
user = None
for attempt in range(3):
name = input("Username? ")
password = input("Password? ")
if name in operators and operators[name] == password:
user = name
break
print("access denied")
if user:
print("welcome", user)
led("green")
forward(50, distance=20)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.