That Blue Square Thing

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:

cakeNumber = int(input("Enter the number of cakes to buy: "))

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.

So:

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:

password = ""
while len(password) < 8:
password = input("Enter a password to use: ")
if len(password) < 8:
print("Enter a password that is long enough please")

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:

email = ""
while not("@" in email):
email = input("Enter an email address: ")
if not("@" in email):
print("Enter a valid e-mail address")

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:

cakeNumber = int(input("Enter the number of cakes to buy: "))

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.

So....

try:
cakeNumber = int(input("Enter the number of cakes to buy: "))
except:
print("That is not a valid number")

To really get this working you need to wrap it within a while loop to continue until the input is valid.

valid = False

while not(valid):
try:
cakeNumber = int(input("Enter the number of cakes to buy: "))
valid = True
except:
print("That is not a valid number")

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.

valid = ""

while valid != "Y":
username = input("Enter a username: ")
print()
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:

valid = False

while not(valid):
codeOne = input("Enter your code: ")
print()
codeTwo = input("Now re-enter your code: ")

if codeOne == codeTwo:
valid = True
else:
print("The codes do not match. Please re-enter them.")