You should be able to:
A brief introduction to this can be found here: 7_3_2
The most common input error that you will come across is a ValueError. This can be seen in the example:
try:
num1 = int(input("Type a number :"))
except ValueError:
print ("You must enter a number")
num1 = int(input("Type a number :"))
try:
num2 = int(input("Type another number :"))
except ValueError:
print ("You must enter a number")
num2 = int(input("Type another number :"))
print (num1+num2)
Try and except has been used to ask the user again if they make an error. This would only work once. It is better to have it set up in a while loop.
dataInvalid=True
while dataInvalid:
try:
num1=int(input("Number :"))
dataInvalid=False
except ValueError:
print ("Type a number")
print ("Thank you")
print ("You entered a number")
A good programmer would also put a count in there giving the user a limit to how many times they can get it wrong.
Another type of check is to ask for a specific word or character from a list of options. For example,
while input().upper() not in ["YES","NO"]:
print ("Type YES or NO")
print ("Thanks")
This code automatically converts an input to capitals so that the user can't get this wrong. It then checks if they have typed YES or NO, if they haven't it will keep asking them.
PROGRAMMING CHALLENGE!
Create a data validation check for a registration plate or a post code.
You need to know how to add a password to your application. The simplest way to do this is with a while loop. The code below gives the user three attempts to get the password right before the account is locked.
password = "four"
checkpass = input("Password :")
attempts = 1
passwordIncorrect = (password != checkpass and attempts<3)
while passwordIncorrect:
print ("invalid")
checkpass = input("Password :")
attempts+=1
passwordIncorrect = (password != checkpass and attempts<3)
if password!=checkpass:
print ("Your account has been locked")
else:
print ("Access Granted")
Try to work out how this piece of code is working and use #hashtags to explain it.
A brief introduction to this can be found here: 7_3_6 re-visit this first.
You need to understand the definitions of:
When you are testing your applications, you must consider each of these test data types. Don't be complacent and use only "normal test data" because this could cause issues for your user.
When you test your applications you should create a test plan. This test plan should cover all types of test data. A test plan for the code in the password authentication example would be:
Can I type in a very long string (over two lines of input)?
" jdskl; djjska l;djskal; djsakl; djkla;JD ;JK;JDL;J;L KSLA; JLS;ADKL; KL; ASJL;S ;lajDL;JK;LAJSL; DKS; JKAL JKL;SJDL;AJ DKSL; SALJDKSAL;d kasLs "
Can I type in nothing?
""
What happens if I get the password wrong three times?
"five"
"five"
"five"
What happens if I get the password wrong two times and right the third time?
"five"
"five"
"four"
What happens if I type in integers?
23214
What happens if I type in real numbers / float?
321.12
What happens if I type a hashtag or a forward slash?
##//
What happens if I get the password right first time?
"four"
You could try all of this test data yourself in Python to see if it still works!
It is good practice to test your programs as you are working on them using these three types of test data.
SOURCE RECOGNITION - PLEASE NOTE: The examination examples used in these walking talking mocks are samples from AQA from their non-confidential section of the public site. They also contain questions designed by TeachIT for AQA as part of the publicly available lesson materials.