Knowledge organisersDefensive Design
Predict invalid input.
Defensive design means writing code that handles unexpected or invalid input gracefully instead of crashing (e.g. wrapping int(input(...)) in try/except). Always assume the user will make mistakes.
# Without defence — crashes on bad input
age = int(input("Age: ")) # Crashes if user types "abc"
# With defence
try:
age = int(input("Age: "))
except ValueError:
print("Please enter a number")