Skip to content
Menu

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.

Whiteboard video lesson: Validation, authentication and random numbers

8-minute video lesson

Teaching, worked examples and 20 practice questions solved step by step.

Watch free for a month

What your child will learn

  • Describe the types of validation check
  • Write validation code
  • Write a simple authentication routine
  • Use random.randint()

The method

  1. Validation: is the input sensible? (not ‘is it correct?’)
  2. Checks: presence, length, range, type, format.
  3. Authentication: confirm identity — username and password.
  4. 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.

  1. Step 1: Value between limits.
  2. Step 2: Range check.

Answer: range check

Question 2

Write a validation loop so that score must be 0–10.

  1. Step 1: c:while score < 0 or score > 10:
  2. 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?

  1. Step 1: Age 15 entered instead of 16.
  2. Step 2: Passes the range check but is wrong.

Answer: no — only that it is sensible