Knowledge organisersDefensive Design
Practical experience of designing input validation and simple authentication (e.g. username and password)
Input validation checks that data entered by a user is reasonable and appropriate before the program processes it. Common types include presence checks (data was entered), range checks (value falls within limits), length checks (correct number of characters), type checks (correct data type), and format checks (correct pattern).
nights >= 1 AND nights <= 5.firstname != "").code.length == 3.if h >= 40 AND h <= 180. NOT: if h >= 40 AND <= 180 (missing variable reference).>= and <= in code, NOT the maths symbols ≥ or ≤ (not available on a keyboard). Also =< is WRONG — must be <=.if room == "basic" or "premium" is WRONG. You must compare the variable each time: if room == "basic" or room == "premium".if room != "basic" or room != "premium" is ALWAYS True (any value will fail one test). Use AND: if room != "basic" AND room != "premium".# Range check
num = int(input("Enter a number 1-10: "))
while num < 1 or num > 10:
print("Out of range!")
num = int(input("Enter a number 1-10: "))
# Presence check
name = input("Enter your name: ")
while name == "":
name = input("Name cannot be blank: ")
# Length check
password = input("Enter password (min 8 chars): ")
while len(password) < 8:
password = input("Too short! Try again: ")