1. understand how to catch errors in a program 2. know how to filter user input to stop incorrect...

10
Input Validation & Errors

Upload: barnaby-harper

Post on 05-Jan-2016

218 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data

Input Validation & Errors

Page 2: 1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data

1. Understand how to catch errors in a program

2. Know how to filter user input to stop incorrect data

Learning Objectives

Page 3: 1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data

Why do you think we need validation?

What happens to a program when a run time error occurs?

Questions – In pairs

Page 4: 1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data

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

Page 5: 1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data

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.

Page 6: 1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data

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”)

Page 7: 1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data

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

Page 8: 1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data

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

Page 9: 1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data

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”)

Page 10: 1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data

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.