1. understand how to catch errors in a program 2. know how to filter user input to stop incorrect...
TRANSCRIPT
Input Validation & Errors
1. Understand how to catch errors in a program
2. Know how to filter user input to stop incorrect data
Learning Objectives
Why do you think we need validation?
What happens to a program when a run time error occurs?
Questions – In pairs
It’s REALLY important stuff!! There are a number of reasons why you need to
validate the data that’s imported into a program
◦ Completion – Ensure that you capture all of the data required. Stops forms etc from being submitted without the required data being empty
◦ Efficiency – Allows the user to fill in forms or inputs accurately
◦ Consistency – Makes sure that you get the same set of data from each user
◦ Error minimisation – Reduces the danger of inputting erroneous or misleading data.
◦ Security – Stops security breaches in data storage
This is why it’s important
What happened?
In 2010 the Royal Navy’s website was attacked, using SQL injection.
This happened because input data wasn’t correctly validated and cleaned.
Admin usernames and passwords where recovered and then posted on the internet, as well as other details.
The basics
The easiest way to validate is to compare the data.
value = input(“Please input a number between 1 and 10”)
If(value >0 && value <11):
#The value that has been input is correct
#More code to do something with the input
else:
#The value isn’t correct
print(“You haven’t entered the correct
information”) print(“Please try again”)
Try statementsTry statements are a way in which you can run code, but catch any run time errors that might occur without the program crashing
try:num = float(input(“Enter a Number”)
except:print(“Something went wrong”)
Slightly more advanced
Exception Type DescriptionIOError Tried to open a non-existent file in read mode
IndexError Raised when trying to access a non-existent element in an array (list)
KeyError Raised when an array key is not found
NameError Raised when a name is not found, i.e. function or variable
SyntaxError Raised when a syntax error is found
TypeError Raised when a function/operation is used inappropriately
ValueError Inappropriate value but right time
ZeroDivisionError Raised when the second argument of a division operation is zero
Exception Types
Exampletry:
num = float(input(“Enter a Number”)Except ValueError:
print(“That was not a number!”)
There are lots of different Exceptions, here are a selection of the mostcommon
You may come across a point where you need to declare multiple exceptions, this is how you do it.
Multiple Exceptions
Example – 1 Exception
try:num = float(input(“Enter a Number”)
Except ValueError:print(“That was not a number!”)
Example – Multiple Exceptions
try:num = float(input(“Enter a
Number”)Except ValueError:
print(“That was not a number!”)Except TypeError:
print(“I can only convert”)
Coding Challenges - Calculator
You need to code a calculator. The calculator is only needs to be able to do simple maths such as +, -, * and /.
To ensure that the calculator program doesn’t crash if the user enters incorrect data, you need to use simple validation and also try statements.