Defensive Design & Validation
Anticipate misuse and reject invalid values before processing them.
Defensive design considers what could go wrong and protects the program against likely misuse. Validation checks whether input is acceptable before it is used.
Predict
Read the code first. Do not run it yet.
choice = input("Y or N: ")
while choice != "Y" and choice != "N":
choice = input("Y or N: ")
print("Accepted")- Which values are accepted?
- Which condition causes the loop to repeat?
- What kind of misuse is being anticipated?
Investigate
Use this code to explain exactly how the program works.
age = int(input("Age 11-16: "))
while age < 11 or age > 16:
age = int(input("Age 11-16: "))
print("Valid age")- What makes a value invalid?
- Why is
orcorrect in the loop condition? - What happens with ages 11 and 16?
Validation
rating = int(input("Rating 1-5: "))
while rating < 1 or rating > 5:
print("Invalid rating")
rating = int(input("Rating 1-5: "))
print("Thank you")- The program checks the allowed range.
- Invalid values trigger another input request.
- Boundary values 1 and 5 are valid.
Self-study guide
Defensive design means anticipating how a program might be misused or given unsuitable data and designing it to cope sensibly. OCR specifically includes anticipating misuse, authentication, input validation and maintainability considerations.
Validation checks whether input satisfies rules. It does not prove that the data is factually correct; it only checks that the data is acceptable according to the program’s criteria.
OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
input validation | while/selection checks in Python | Reject data that breaks stated rules. |
range check | value < minimum or value > maximum | Checks numerical boundaries. |
length check | len(text) | Checks the number of characters. |
anticipating misuse | defensive conditions and limits | Think about likely incorrect or hostile input. |
Worked example
A validation loop keeps requesting data until the rule is satisfied.
mark = int(input("Mark 0-100: "))
while mark < 0 or mark > 100:
print("Invalid mark")
mark = int(input("Mark 0-100: "))
print("Accepted", mark)Accepted 72
- The first input is checked immediately.
- Values below 0 or above 100 are invalid.
- The loop repeats only while the value breaks the rule.
- Boundary values 0 and 100 are valid and therefore accepted.
Another example
A string rule can be validated in the same way.
username = input("Username: ")
while len(username) < 4 or len(username) > 12:
username = input("Username: ")
print("Username accepted")Username accepted
- The rule concerns length rather than numerical value.
- The program anticipates inputs that are too short or too long.
Common mistakes
- Calling validation “verification”. Verification is a different idea; OCR’s required defensive-design list here focuses on validation.
- Using
<instead of<=carelessly at boundaries. - Validating after the value has already been used in an important calculation.
- Checking only one side of a range.
Exam-ready checklist
- Explain defensive design and anticipating misuse.
- Design range and length validation.
- Choose correct boundary conditions.
- Explain how invalid data should be handled.
What you have learned
- Defensive design anticipates likely misuse.
- Validation checks input against rules.
- A loop can repeatedly request input until it is valid.
- Boundary values need careful consideration.