Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data."— Presentation transcript:

1

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

3  Why do you think we need validation?  What happens to a program when a run time error occurs?

4  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

5 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.

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

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

8 Exception TypeDescription IOErrorTried to open a non-existent file in read mode IndexErrorRaised when trying to access a non-existent element in an array (list) KeyErrorRaised when an array key is not found NameErrorRaised when a name is not found, i.e. function or variable SyntaxErrorRaised when a syntax error is found TypeErrorRaised when a function/operation is used inappropriately ValueErrorInappropriate value but right time ZeroDivisionErrorRaised when the second argument of a division operation is zero Example try: 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 most common

9 You may come across a point where you need to declare multiple exceptions, this is how you do it. 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”)

10  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.


Download ppt "1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data."

Similar presentations


Ads by Google