AQA Computer Science GCSE
Programming project - Validation
Validation is the art of ensuring that the data that users enter is valid - i.e. it's correct and won't break your program or produce unexpected outcomes.
So, for example, you need users to enter the number of cakes they want to buy. It's reasonable to expect that they're going to need to enter a whole number (an integer), so in Python you'd get the input and then convert it into an integer straight away:
That's all very good, but the problem is that users are stupid.
Actually, that's not true. Most users aren't stupid. Most users know what to do and try to do it well enough to not crash your program (or your webform or whatever). But occasionally they make mistakes. Or sometimes one of them really is stupid.
- if the user enters "one" as a string, your program crashes
- if they enter "banana" because they thought they were asking for a type of cake, it crashes
- if they press the return key and enter an empty string ("") then it crashes
- if they enter 3.142 (which is much more suitable for pies rather than for cakes) then the program crashes
So, you need to validate user input. This simply makes sure that whatever your user enters is a valid piece of data.
How to validate
There are different ways to go about validating user input. If, for example, you want to ensure the password entered has at least 8 characters in it, you can use something like:
while len(password) < 8:
if len(password) < 8:
The IF block in the code above is the helpful error message that should, hopefully, tell the user what they've done wrong. Do try and include this if you can please.
Another example is if you need an e-mail address entered. This has to include an & symbol, so you can validate for that as well:
while not("@" in email):
if not("@" in email):
This is quite complex and uses the Boolean operator not to basically say "if there's NOT an & in the email address then..." It does work though.
Using Try – Catch to stop crashes
One really clever way to stop crashes is to use an idea called Try – Catch (or Try – Except).
This solves the sort of problem you get when you're using code such as:
We know that this will cause a crash if the user doesn't enter an integer. So, what to do? None of the other methods will really work, because as soon as we use the int() function the program will crash if it's not an integer.
To really get this working you need to wrap it within a while loop to continue until the input is valid.
valid = True
The way this works is that the program is told the try the bit in the try block and if there's an error - technically an "exception is thrown" - the except block is executed.
Note that I've called this Try – Catch rather than Try – Except. In other programming languages the code uses a "catch" command rather than Python's "except" (short for "exception"). My gut feeling is that the idea is better known by programmers as Try – Catch, that's all.
A word about Verification
You can't validate everything.
A name isn't really able to be validated. A middle name certainly can't be - some people don't have one, some people have three. So how can we help users enter valid data?
But we can give people the chance to check the data they've entered. This is called Verification - the user verifies that the data is correct.
while valid != "Y":
print("You entered " + username)
valid = input("Is this correct: ").upper()
So, if the user enters "y", the verification is met and the program moves on. Any other entry and the program asks for another input. Note that .upper() simply turns any input into uppercase.
Another way to verify input is to ask the user to enter the same thing twice and then check that the two things match. This is often done when setting a password, for example.
Code for this might look like:
codeTwo = input("Now re-enter your code: ")
if codeOne == codeTwo: