Professional Documents
Culture Documents
Validation
Validation
Validation
1. Complete the table to show the type of check required, and to provide an example of valid input data, and an example of
invalid input data.
Type of check
Description Valid input Invalid input
required
Numbers only
A phone number must be given. 89725845 B768657836
8 digits length
Number
January = 1, February = 2, … December = 12. One character 6 67b
1-12
Numbers only
A phone number must be 11 characters long. 58569712560 68
11 characters long
2.
3. Complete the table to identify the type of check used in each of the examples of code given.
Range
Look up check
Presence check
Presence check
Length check
4. 5. Complete the code below to implement the type of validation shown in the right-hand column.?
#Activity 3a
name = input ("Spell the number 5:")
if ( ):
print ("Invalid")
yearGroup = int (input ("Enter your year group: "))
if ( ):
print ("Invalid")
favourite = int ( input( "Enter your favourite subject: "))
if ( ):
print ("Invalid")
flavour = input ("Enter C for chocolate or V for vanilla: ")
if ( ):
print ("Invalid")
choice = input ("Enter 'yes' or 'no': ")
if ( ):
print ("Invalid")
name.isdigit() == True
favourite.isdigit == True
6. 7. Copy the code and put it into the correct order to implement validation.
print (theMenu)
print (theMenu)
print ("Please choose from the menu.")
while theChoice not in theOptions:
theOptions = ["C", "V", "S", "Q"]
theChoice = input ("Choice: ")
theChoice = ""
theChoice = input ("Choose a flavour: ")
theMenu = ("Choose a flavour from the menu: ") +
"\nC Chocolate" +
"\nV Vanilla" +
"\nS Strawberry" +
"\nQ Quit")
8. 9. Cope the program below into your development environment, save it as “Activity3c Finished” and complete the
code to implement validation
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
colour = ""
teenager = 0
userThere = ""
sweet = 0
choice = ""
sweetOptions = ["1", "2", "3"]
# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
colour = input ("What colour is grass, normally? ")
# =====> Complete the length check
# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
colour = input ("What colour is grass, normally? ")
if (len(colour) > 7 or len(colour) < 2):
print ("Inavid")
10. 11. Cope the program below into your development environment, save it as “Activity3d Finished” and complete the
code to implement validation
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
theChoice = input ("Choose a colour: ")
while theChoice not in theOptions:
print (theMenu)
theChoice = input ("Choice: ")
theMenu = ("Choose a colour from the menu:" +
"\nR Red" +
"\nG Green" +
"\nB Blue" +
"\nQ Quit")
print (theMenu)
theChoice = ""
theOptions = ["R", "B", "G", "Q"]
print ("You chose " + theChoice)