Preview

11 - Validation

 1. For the following python code, what would happen if the user entered '303'
def check_input(min, max):
    prompt = "Enter an integer number between %d and %d: " % (min, max)
    value = int(input(prompt))
    while (value < min or value > max):
        value = int(input(prompt))
    return value

check_input(1,100)

  It would ask the user if the number was greater than or less than 1 or a 100

  It would print the number 303

  It would print: "Enter an integer number between 1 and 100:" to prompt the user again

  It would accept the input as it is a valid number

 2. Validation is an automatic computer check to ensure that the _________________________. It does not check the accuracy of data

  data entered is sensible and reasonable

  data entered is string only

  data entered is correct and exact

  data entered is only numeric

 3. A secondary school student is likely to be aged between 11 and 16. Validation would accept numbers between 11 and 16.

  This is a string input check

  This is a presence check

  This is a format check

  This is a range check.

 4. A field has been validated to accept numbers between 1 to 100. An input of 3 would be…

  accepted as it is valid data

  accepted only if the individual's age is 3 (and denied if not)

  None of the above

  denied

 5. What is the name of the following type of validation?
looks up acceptable values in a table

  check digit

  look up table / drop down menu

  Spell check

  presence check

 6. Analyse the following code and explain what it will do if the user enters the word 'age'
def inputNumber(message):
  while True:
    try:
       userInput = int(input(message))       
    except ValueError:
       print("Not an integer! Try again.")
       continue
    else:
       return userInput 
       break 
     
 
#MAIN PROGRAM STARTS HERE:
age = inputNumber("How old are you?")

  It will reject the data without providing a message

  It will accept it as valid input

  It will ask the user to enter a higher number

  It will present the message: "Not an integer. Try again" and ask the user how old they are again

 7. In the following code, on what line is the validation occuring and what is it doing?
def main():
   number=input("Enter a number:")
   if number.isdigit():
     print("Valid input - thank you")
   else:
    print("Invalid")
main()

  Line 4 - it is checking to see that the entered data is text or numeric only

  Line 3 - it is checking to ensure that the entered data is a digit

  Line 6 - It will print "Invalid" if the data input is invalid and is a number

  None of the above

 8. In this incredibly simplistic email validation code, what could you add to line 3 to check for an @ sign?
def main():
   email=input("Enter an email:")
   #what could you add here to check for an @ sign
     print("Valid email - thank you")
   else:
    print("Invalid email - no @ sign!")
main()

  for @ in emails:

  if "@" in email:

  if @ found in email:

  if an @ sign in email then:

 9. The following is an example of validation: Range check: Checking if a number entered is between two numbers

  False

  True

 10. This code uses regular expressions for validation. What would the output/result be if the input is: "ssdf"?
import re 
def main():
          password= input("Create a password : ")
          x = True #random variable x which is set to TRUE
          while x:  #while x is TRUE,then run the following which is in the loop
              if (len(password)<6 or len(password)>12): #if password not between 6 and 12 exit loop
                  break #exit the WHILE LOOP
              elif not re.search("[a-z]",password): #if password doesn't contain one lowercase letter
                  print("You need at least one lower case letter")
                  break
              elif not re.search("[0-9]",password): #if pasword doesn't contain numbers
                  print("You need at least one number")
                  break
              elif not re.search("[A-Z]",password): #if password doesn't contain upper case letter
                  break
              elif not re.search("[$#@]",password): #if password doesn't contain character
                  break
              elif re.search("\s",password): #if contains blank space
                  break
              else:
                  print("Valid Password")
                  x=False #Valid password has been created, so set x to False and exit the loop
                  break

          if x: #note this is outside the while loop (so the code jumps to it, if BREAK is typed inside the loop
              print("Not a Valid Password")
              main()

main()

  Valid Password

  Not a Valid Password followed by Create a Password:

  No digits allowed

  Create a new password please