Course 7.3Lesson 3 of 3
A program must not trust the data a person types. Validation checks that data is sensible: range, length, type, presence and format checks, and the check digit. Verification checks that data was entered as intended: a visual check and a double entry check. You write the checks in pseudocode and Python.
Try the code and the simulations beside the lesson.
A pupil types her age as . Someone leaves the surname box empty. A shop assistant types a price with a letter in it. If a program accepts data like this, every result after it is wrong. So a program checks its input before it uses it.
Validation is an automatic check, done by the program, that data is sensible and follows the rules. It cannot tell whether the data is true: an age of passes a range check even if the pupil is really . The syllabus names six validation checks.
AB1234.A range check usually sits in a loop that repeats the input until the value is acceptable. The prompt and an error message help the user.
REPEAT
OUTPUT "Enter a mark from 0 to 100"
INPUT Mark
IF Mark < 0 OR Mark > 100
THEN
OUTPUT "Error: the mark must be 0 to 100"
ENDIF
UNTIL Mark >= 0 AND Mark <= 100A length check uses LENGTH.
INPUT Password
IF LENGTH(Password) < 8
THEN
OUTPUT "Too short: use at least 8 characters"
ELSE
OUTPUT "Accepted"
ENDIFA presence check compares the input with the empty string "".
INPUT Surname
IF Surname = ""
THEN
OUTPUT "Please enter your surname"
ENDIFHere is one common method. Each digit is multiplied by a weight, and in turn, and the products are added. The check digit is the number that must be added to make the sum a multiple of . The book numbers called ISBN-13 use this method.
For the code the weighted sum is . Adding makes . So the check digit is , and the full code is .
If one digit is typed wrongly, the sum changes and the check digit no longer matches. Swapping two neighbouring digits usually changes the sum too, because they have different weights.
Verification checks that data has been entered as intended, that it matches what the user meant or what was on the paper. The syllabus names two checks.
REPEAT
OUTPUT "Enter your email address"
INPUT Email
OUTPUT "Enter it again"
INPUT Again
IF Email <> Again
THEN
OUTPUT "They do not match: try again"
ENDIF
UNTIL Email = AgainValidation and verification work together. Validation stops data that cannot be right. Verification stops mistakes in data that looks sensible.
The grey blocks marked pseudocode are written in the exam style and do not run. The Python box is the same algorithm in Python, and that one runs. The Python program has one function for each check, and works out the check digit of the code in the lesson.
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place in the course.