Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Data types, validation and structures

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

50 minutes

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

WordWhat it means
Data validationChecking that data is sensible before the program uses it.
Presence checkChecks that something was entered.
Range checkChecks that a number is between a lowest and a highest value.
Length checkChecks that text is not too short or too long.
Type checkChecks that the data is the right kind, such as a number.
Format checkChecks that the data has the right pattern, such as an email address.
Data structureA way to organise many values, so the program can use them well.
StackA structure where the last item in is the first out.
QueueA structure where the first item in is the first out.

The cells students fill in are left blank in this preview.

Five validation checks

CheckQuestion it asksPassesFails
PresenceIs it empty?Sam(nothing)
RangeIs it between 0 and 120?16-5, 200
LengthIs it 3 to 12 characters?sam2026ab
TypeIs it a whole number?16banana
FormatDoes 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Try it: a validator

Code: python
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

WordWhat it meansExample
Data typeThe kind of value, such as a whole number or text.
IntegerA whole number with no decimal point.16
FloatA number with a decimal point.2.50
StringText."Sam"
BooleanTrue or False.
ArrayA list of values in order, found by their position.
TupleA fixed group of values that cannot change.
DictionaryPairs 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

FieldData typeWhy
AgeIntegerA whole number
Price in poundsFloat, or integer penceHas a decimal point. Pence avoid rounding errors
Phone numberStringYou never add phone numbers. It can start with 0
Is a memberBooleanOnly yes or no
NameStringText

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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The lesson goes on

6 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.