Learners will be able to:
- Understand what Validation is and how it is used
- Learn and code validation in Python
- Apply understanding to practice validation
In this lesson you will learn about validation and the requirement for validation in programming solutions. You will learn how to apply and use validation code to 'validate' the data that is inputted or outputted into a program.
Part 1: What is Validation:
Validation is an automatic computer check to ensure that the data entered is sensible and reasonable. However it does not check the accuracy of data. For example, a secondary school student is likely to be aged between 11 and 16. The computer can be programmed only to accept numbers between 11 and 16, it cannot check that the person is lying about their age.
There are several types of Validation:
Part 2: A Basic Example of Validation:
Part 1: What is Validation:
Validation is an automatic computer check to ensure that the data entered is sensible and reasonable. However it does not check the accuracy of data. For example, a secondary school student is likely to be aged between 11 and 16. The computer can be programmed only to accept numbers between 11 and 16, it cannot check that the person is lying about their age.
There are several types of Validation:
- Range check: check numbers to ensure they are within a range of possible values, e.g., types of the value for month should lie between 1 and 12.
- Reasonable check: check values for their reasonableness, e.g. (age > 16) && (age < 100)
- Arithmetic check: check variables for values that might cause problems such as division by zero.
- Format check: check that the data is in a specified format (template), e.g., dates have to be in the format DD/MM/YYYY
Part 2: A Basic Example of Validation:
- Look at the code below and run it, what is the problem with it?
- Create a code which asks for a user's phone number and validates / checks the number so that it only accepts numbers.
- You can use the code print type(username) to find out the data type of the variable. Add this line to the program below, what does it do?
Part 3: Better Validation:
- A better method of checking data is to check that it is the correct data type. For example "Dave" should be text, while "23536" should be an integer.
- The example below shows how you can combine this with a Try and Except.
- Add an appropriate response or message if the user enters in an number
- How would you validate a float number?
Part 4: Data Types
- Checking that the correct type of data is useful for checking for errors as well as know the data type, run the program below.
- What happens if you change the random_data to an integer?
- Add code to check that an integer has been entered and return an error if not.
Part 5: Digit check
- A digit check can be used to check that the data entered is a number, well a digit, run the code below, enter in 5 numbers, what happens, why?
- The important code line is, if number.isdigit(): this checks the number is a digit!
- Now run it again, enter text instead, what happens?
- Add new lines of code to return a suitable message saying the user has entered text.
Part 6: Checking that it is text that is entered.
- Python can also check to ensure that text has been entered by using the islower(), Run the code below, what happens?
- Now enter Upper case letters, what happens?
- Add a suitable print statement to tell the user they entered Uppercase letters
- Now change the code to check for Upper case letters, does it work?
- Finally add a code line to respond appropriately if digits are entered,
Part 7: Entering nothing!
- What if a user enters no data? How do you check this?
- Run the program below and enter nothing, what happens?
- Create a loop so that while no data is entered the program keeps checking, it ends when the user enters in some data.
Part 8: The length check...
- Code can also be used to check the length of the data that is entered, can you think where this might be useful?
- Run the code below and test it.
- What are the issues with the program?
- The word should be 10 letters long, change the code and the validation checks to reflect this.
Hacker Test:
Can you hack? try this:
Create a simple program to check that a 8 digit password is entered and it must have:
Can you hack? try this:
Create a simple program to check that a 8 digit password is entered and it must have:
- Two upper case letters
- Two lower case letters
- Two digits
Plenary
- Make a poster about all the validation checks that you have learnt about
- Research other methods of validation, what are they.
- Create a short video / animation about how to code validation into a program.
- Write a rap about validation, code and its use.