Preview

06 - Validation Routines

 1. What is Validation?

  the action of checking the validity of the objects into which the data is put (e.g. their secure fixation)

  the action of ensuring that hackers are unable to access any data from an input box

  the action of checking or proving the absolute accuracy of something

  the action of checking or proving the validity or accuracy of something

 2. In coding, buildinng in validation is extremely important. E.g. Facebook uses validation on sign up by….

  Providing a drop down box of valid years to select a user's date of birth

  Providing a range of usernames to select from (i.e ones that already exist)

  Providing each user with a predefined username and other details such as gender

  None of the above

 3. There are several types of validation such as length check, presence check, range check. Presence check is….

  checking to see that the field is filled with the word 'presence'

  checking to see that data has been entered into the field (it hasn't been left blank)

  checking to see that the field accepts only images or packets (e.g. presents)

  checking to see that the field has been left blank

 4. A company website wants age to be entered as a whole number. What will happen if the user enters "f" in the following program?
n = None
while type(n) is not int:
   try:
      n = input("Please enter an age: ")
      n = int(n)
      print("You entered: %d" % n)
   except ValueError:
      print("%s is not an integer.\n" % n)

  It will print "Please enter an integer:" as 'f' is not an integer

  It will cause an error and crash the proram without printinng anything because of the 'except'

  It will print "Please enter an age:" as 'f' is not an age

  It will accept the input and then end the program with the words "Value Error"

 5. To validate the user entry and ensure that is a number it is possible to "_______________" when it occurs using the try…except….else block as follows:
try:
    value=int(input("Type a number:"))
except ValueError:
    print("This is not a whole number.")

  try all possible values

  try different values

  try this exception

  catch this exception

 6. Analyse the following code that employs validation to ensure the user enters an integer. What happens if they enter '33'?
def inputSecretCode(message):
  while True:
    try:
       userInput = int(input(message))       
    except ValueError:
       print("You are clearly not a secret agent. Invalid code!")
       continue
    else:
       return userInput 
       break 
     

#MAIN PROGRAM STARTS HERE:
secretcode = inputSecretCode("Enter your secret code?")

if (secretcode==0001):
  print("Welcome agent 0001 - you are the agent we've been looking for.")
else:
  print("You're an agent - but not who we are looking for")

  It will print: "You're an agent - but not who we are looking for"

  It will print: "Welcome agent 0001 - you are the agent we've been looking for."

  It will cause an error as '33' is not "0001"

  None of the above

 7. In the following code snippet a password has to be in the correct format. Which of the following would be valid?
# Python program to check validation of password
# Module of regular expression is used with search()
import re
password=input("Enter password:")
# example: this is a valid password = "R@m@_f0rtu9e$"
flag = 0
while True:  
    if (len(password)<8):
        flag = -1
        break
    elif not re.search("[a-z]", password):
        flag = -1
        break
    elif not re.search("[A-Z]", password):
        flag = -1
        break
    elif not re.search("[0-9]", password):
        flag = -1
        break
    elif not re.search("[_@$]", password):
        flag = -1
        break
    elif re.search("\s", password):
        flag = -1
        break
    else:
        flag = 0
        print("Valid Password")
        break
 
if flag ==-1:
    print("Not a Valid Password")

  331231Abc

  pB125643#h@

  331231Abc

  34La3#223A

 8. Why would "AaBb$Cc" not be a valid password according to the python code's implemented validation?
# Python program to check validation of password
# Module of regular expression is used with search()
import re
password=input("Enter password:")
# example: this is a valid password = "R@m@_f0rtu9e$"
flag = 0
while True:  
    if (len(password)<8):
        flag = -1
        break
    elif not re.search("[a-z]", password):
        flag = -1
        break
    elif not re.search("[A-Z]", password):
        flag = -1
        break
    elif not re.search("[0-9]", password):
        flag = -1
        break
    elif not re.search("[_@$]", password):
        flag = -1
        break
    elif re.search("\s", password):
        flag = -1
        break
    else:
        flag = 0
        print("Valid Password")
        break
 
if flag ==-1:
    print("Not a Valid Password")

  Because it is not at least eight characters long

  Because it has only three characters in it (A, B and C) and it needs eight

  Because it does not have any binary numbers

  Because it has more than one upper case letter

 9. isDigit() is a python validation method that lets you check for a digit. What will the following return? (What is the output?)
s = "28212"
print(s.isdigit())

  TRUE

  1

  28212

  FALSE

 10. Given the following code, what will the output be if the user enters: "11"
s=input("Enter string:")
if s.isdigit()==True:
  print("You input digits!")
else:
  print("You did not enter all digits")

  You input digits

  You did not enter all digits

  Error - as digits are not input -it says "Enter String"

  None of the above