What if they type "banana"?
Your form asks for age. One user types 16. Another types -5. Another types banana. Another leaves it empty.
A good program does not crash. It does not accept wrong data. It tells the user what to fix.
You plan this at the design stage.
Key words
| Word | What it means |
|---|---|
| Data validation | Checking that data is sensible before the program uses it. |
| Presence check | Checks that something was entered. |
| Range check | Checks that a number is between a lowest and a highest value. |
| Length check | Checks that text is not too short or too long. |
| Type check | Checks that the data is the right kind, such as a number. |
| Format check | Checks that the data has the right pattern, such as an email address. |
| Data structure | A way to organise many values, so the program can use them well. |
| Stack | A structure where the last item in is the first out. |
| Queue | A structure where the first item in is the first out. |
The cells students fill in are left blank in this preview.
Five validation checks
| Check | Question it asks | Passes | Fails |
|---|---|---|---|
| Presence | Is it empty? | Sam | (nothing) |
| Range | Is it between 0 and 120? | 16 | -5, 200 |
| Length | Is it 3 to 12 characters? | sam2026 | ab |
| Type | Is it a whole number? | 16 | banana |
| Format | Does it look like an email address? | [email protected] | sam.mail.com |
The cells students fill in are left blank in this preview.
Quick check: which check?
Have a go
Quick check: which check?
4 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
Try it: a validator
def check_mark(text):
if text == "":
return "Please type a mark"
if not text.isdigit():
return "A mark is a whole number"
mark = int(text)
if mark > 100:
return "The highest mark is 100"
return "ok"
for typed in ["", "abc", "150", "72"]:
print(repr(typed), "->", check_mark(typed))
# Try a mark of "-3". What does the program say? Why?
# Add "-3" to the list and run it again.
Validation is not verification
data-callouts.important.label
Validation checks that data is sensible. A date of birth in the future fails. It cannot check that the data is true. A wrong but sensible age passes. Verification means checking that data is correct, for example by asking the user to type it twice.
More key words: types and structures
| Word | What it means | Example |
|---|---|---|
| Data type | The kind of value, such as a whole number or text. | |
| Integer | A whole number with no decimal point. | 16 |
| Float | A number with a decimal point. | 2.50 |
| String | Text. | "Sam" |
| Boolean | True or False. | |
| Array | A list of values in order, found by their position. | |
| Tuple | A fixed group of values that cannot change. | |
| Dictionary | Pairs of a name (key) and a value, so you can look a value up by its name. |
The cells students fill in are left blank in this preview.
Choose a data type for each field
| Field | Data type | Why |
|---|---|---|
| Age | Integer | A whole number |
| Price in pounds | Float, or integer pence | Has a decimal point. Pence avoid rounding errors |
| Phone number | String | You never add phone numbers. It can start with 0 |
| Is a member | Boolean | Only yes or no |
| Name | String | Text |
The cells students fill in are left blank in this preview.
Choose the type of each field
Have a go
Choose the type of each field
6 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.