Revise Computing
Revision notesQuestionsFor schools
Log inStart revising free
Revision notesQuestionsFor schoolsLog in
GCSE Computer Science/Supplementary programming practice/Defensive Design

Validation subprograms

301.16 Defensive Design

Practise exam questions

Defensive Design — Note 4 of 5

Reusable validation routines.

What you need to know

Writing validation as a function makes it reusable. A validation function takes input, checks it, and either returns the valid value or keeps asking until it's correct.

What to know

  • Put validation logic in a function for reuse
  • Function can loop until valid input is received
  • Return the validated value to the caller
  • Can be called from anywhere in the program

Code examples

Validation function
python
def get_int_in_range(prompt, low, high):
    value = int(input(prompt))
    while value < low or value > high:
        print("Must be " + str(low) + "-" + str(high))
        value = int(input(prompt))
    return value

age = get_int_in_range("Age: ", 0, 120)
← Previous noteAuthentication301.16 Defensive DesignNext note →Error messaging301.16 Defensive Design

Back to Defensive Design

Specification notes for programming are in Paper 2.

Defensive Design

Note 4 of 5

  1. 1Anticipate misuse
  2. 2Validation checks
  3. 3Authentication
  4. 4Validation subprograms
  5. 5Error messaging
Topic overview

© 2026 Revise Computing · revisecomputing.co.uk

Privacy noticeTermsContact

Revise Computing is an independent revision resource. It is not affiliated with, or endorsed by, OCR (Oxford Cambridge and RSA Examinations).