Preview

03 - Coding Validation Routines (vb.net and python

 1. Validation cannot check that the data entered is correct but only that it is a reasonable and valid input

  FALSE

  TRUE

 2. Analyse the python code below and feel free to try it out for yourself. What type of validation check is it?
#Validation
IsNamePresent=False
while IsNamePresent==False:
  ans=input("Please enter your name:")
  if len(ans) > 0:
    IsNamePresent = True
    print("Thank you for entering your name!")
    

  Range check

  Presence Check

  Format Check

  None of the above

 3. What would you need to change in the code below to make it accept a minimum of three characters for the name?
#Validation
IsNamePresent=False
while IsNamePresent==False:
  ans=input("Please enter your name:")
  if len(ans) > 0:
    IsNamePresent = True
    print("Thank you for entering your name!")
    

  Change line 5 to: if len(ans)>=3:

  It cannot be done

  Change the first line to IsNamePresent = 3

  None of the above

 4. What will happen if you change the first line to IsNamePresent=True
#Validation
IsNamePresent=True
while IsNamePresent==False:
  ans=input("Please enter your name:")
  if len(ans) > 3:
    IsNamePresent = True
    print("Thank you for entering your name!")

  None of the above

  The program will not run. It needs to be False for the program to run (the next line checks for this)

  It will output

  It will run as usual and perform a presence and range validation

 5. This is a validation check that uses regular expressions. What is the purpose of the program?
import re #this stands for import regular expressions

def main():
          
  valid_password = False #random variable x which is set to TRUE
  while valid_password==False:  
    password= input("Create a password : ")
    if not re.search("[0-9]",password): 
          print("You need at least one number")
    else:
          print("Valid Password")
          valid_password = True
          break
    
main()

  To ensure that the input has at least two numbers in it

  To ensure that the input is all numbers

  To ensure that the input has all text

  To ensure that the input has alteast one number in it

 6. In the following code, the input for an email is being validated. If "f@" is entered, what happens?
import re #this stands for import regular expressions

#this is some basic validation to check for an @ sign
def main():
          
  email=input("Enter a valid email address: ")
  if "@" in email:
    print("That is a valid email, thank you")
    main()
  else:
    print("You are missing an @ sign")
    main()
    
main()
 
 

  It will not accept it as it also needs a .com at the end

  It accepts it but reminds the user that it is complete

  It accepts it as a valid email, because the validation is only checking for the @ sign

  None of the above

 7. Analyse the code below. What would the output be if the user entered: "abc"
import sys #this is in order to use the sys.exit() command to exit the program
from random import* #this is needed in order to work with a random number
compguess=7 #7 could be used for testing, or use a random number generator below
#compguess=randint(1,10)
myguess=0
nooftries=0


def main():
          myguess=(input("Enter a number between 1 and 10:"))
          while myguess.isdigit():
                    myguess=int(myguess)
                    if myguess < 10 and myguess >0:
                                        global nooftries
                                        nooftries=nooftries+1
                                        clues(myguess,nooftries)
                    else:
                                        print("Invalid Range")
                                        main()
          print("Only integers please")
          main()


def clues(myguess,nooftries):
          if myguess >compguess:
                    print("Too high")
                    main()
          elif myguess<compguess:
                    print("Too low")
                    main()
          elif myguess==compguess:
                    print("Yep, that's it. You got it in", nooftries, " tries!")
                    print("*********************Goodbye!************************")
                    sys.exit()


main()

  "only one letter please"

  "Invalid range"

  "only numbers please"

  "only integers please"

 8. Analyse the following code: If the user input is '2', the output will be:
def inputNumber(message):
  while True:
    try:
       userInput = int(input(message))       
    except ValueError:
       print("That is not an integer is it?")
       continue
    else:
       return userInput 
       break 
     

#MAIN PROGRAM STARTS HERE:
age = inputNumber("Enter your age:?")
if (age>=18):
  print("You are old enough to vote.")
else:
  print("You will be able to vote in " + str(18-age) + " year(s).")

  That is not an integer is it?

  You will be able to vote in 16 year(s).

  You are not eligible to vote

  Please enter a valid number

 9. What is this a definition of? Checks that the data entered is of an expected type, e.g. text or a number

  Binary Check

  Value Check

  Type Check

  Font Check

 10. What is this? Checks that the user has at least inputted something, stopping them from accidentally entering nothing

  Value Check

  A presence check

  Binary Check

  A Type check