Computer Science Essentials, Unit 4, Lesson 4 of 5

Validation, authentication and strings

GCSE Computer Science, Onscreen programming, 40 minutes

Sign in

Adding length, range, presence, type and format checks to a program, re-asking until the input is valid, logins with stored details and a limited number of attempts, and string handling with slices, case and character codes.

What you will learn

  • Add length, range, presence, type and format checks
  • Amend a program so it re-asks until the input is valid
  • Check a username and password against stored details with a limit on attempts
  • Build and change strings with slices, upper, lower, ord and chr

Validation in a program

Validation checks that input is sensible before the program uses it.

It cannot check that the data is true.

  • Presence check: something was entered: text != "".
  • Length check: len(text) is within limits.
  • Range check: a number is between two limits.
  • Type check: the input is the right kind of data. code.isdigit() is True when every character is a digit, so check it before int(), which would crash on letters.
  • Format check: the text follows a pattern, such as one @ with a full stop after it.

To keep asking until the input is valid, read once before the loop and again at the end of each pass:

n = int(input())
while n < 1 or n > 12:
    print("Out of range")
    n = int(input())

The rest of this lesson and its 10 practice questions are locked.

In this lesson

  1. Validation in a program
  2. Authentication
  3. String handling
  4. Try it: Length checks

10 practice questions, marked as you go; about 40 minutes in all.

Key terms

Validation, Authentication

In this unit: Onscreen programming

Partly written Python programs to complete, amend, fix and extend, checked by running them against tests: subroutines, 2D lists, records from files, validation, strings and longer tasks in parts.

All of Onscreen programming