Year 10 (ages 14–15, Grade 9 in the US) · Computing · GCSE programming · Lesson 12
Validation, authentication and random numbers
Defensive design in Python: validation checks (presence, length, range, type and format), simple authentication with usernames and passwords and limited attempts, and generating random numbers with the random module.
Exam boards: this lesson matches the AQA and Pearson Edexcel GCSE computing courses. Check which course your school follows.

8-minute video lesson
Teaching, worked examples and 20 practice questions solved step by step.
Watch free for a monthWhat your child will learn
- Describe the types of validation check
- Write validation code
- Write a simple authentication routine
- Use random.randint()
The method
- Validation: is the input sensible? (not ‘is it correct?’)
- Checks: presence, length, range, type, format.
- Authentication: confirm identity — username and password.
- import random; random.randint(a, b) includes both ends.
Key words: validation, range check, authentication, defensive design, random.
Worked examples
Worked examples from the lesson, with every step.
Question 1
Which check? Making sure a day of the month is 1 to 31.
- Step 1: Value between limits.
- Step 2: Range check.
Answer: range check
Question 2
Write a validation loop so that score must be 0–10.
- Step 1: c:while score < 0 or score > 10:
- Step 2: c: score = int(input("0-10: "))
Answer: while score < 0 or score > 10: ask again
Question 3
Does validation guarantee the data is correct?
- Step 1: Age 15 entered instead of 16.
- Step 2: Passes the range check but is wrong.
Answer: no — only that it is sensible