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

  None of the above

  Presence Check

  Format Check

 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!")
    

  It cannot be done

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

  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!")

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

  It will output

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

  None of the above

 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 alteast one number in it

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

  To ensure that the input has all text

  To ensure that the input is all numbers

 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 accepts it but reminds the user that it is complete

  None of the above

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

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

 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 integers please"

  "only one letter please"

  "Invalid range"

  "only numbers please"

 8. The following VB.Net console program also checks for username and password - how could the authentication be improved?
Dim username As String Dim password As Integer username = Nothing password = Nothing Console.WriteLine("Enter Your UserName") username = Console.ReadLine() Console.WriteLine("Enter Your Password") password = Console.ReadLine() If username = "Asim" And password = 243 Then Console.WriteLine("Welcome Asim!") Else Console.WriteLine("Access is denied") End If Console.ReadLine() 

Read more at: https://www.thecodingguys.net/tutorials/visualbasic/vb-if-statement

  Allow the number of tries to be limitless

  Limit the number of tries to three

  Force all usernames and passwords to be the same

  None of the above

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

  Value Check

  Type Check

  Binary Check

  Font Check

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

  Binary Check

  A presence check

  A Type check

  Value Check