Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

3.

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

Personal Identification Numbers (PIN) must be 4 digits long


7645 7895205fg
4 digits. Numbers only

Email addresses can only contain alphabetic Numbers and letters


characters, digits 1 to 9, and the symbols . and only hello@bob.com Nigel
@. Contain @ and .

A login name must not be blank. More than 1 character bob

A noughts and crosses game uses the symbols


If it is X and O x helllo
‘X’ and ‘O’.

A computer program uses a menu system with


It is only A B C Q Q f
choices A, B, C, and Q.

2.
3. Complete the table to identify the type of check used in each of the examples of code given.

Code Type of check

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

Code Type Validation

name.isdigit() == True

(yearGroup<1 or yearGroup >


13)

favourite.isdigit == True

flavour != "C" and flavour !=


"V"

choice != "yes" and


flchoiceavour != "no"

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

theMenu = ("Choose a flavour from the menu: " +


"\nC Chocolate" +
"\nV Vanilla" +
"\nS Strawberry" +
"\nQ Quit")
theChoice = ""
theOptions = ["C", "V", "S", "Q"]
Code
print (theMenu)
print ("Please choose from the menu.")
theChoice = input ("Choice: ")

while theChoice not in theOptions:


print (theMenu)
theChoice = input ("Choose a flavour: ")
theMenu = ("Choose a flavour from the menu: " +
"\nC Chocolate" +
"\nV Vanilla" +
"\nS Strawberry" +
"\nQ Quit")
theChoice = ""
theOptions = ["C", "V", "S", "Q"]
Ordered
print (theMenu)
print ("Please choose from the menu.")
theChoice = input ("Choice: ")

while theChoice not in theOptions:


print (theMenu)
theChoice = input ("Choose a flavour: ")

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

teenager = int (input ("How old is a teenager? "))


# =====> Complete the range check

userThere = input ("Are you there? ")


# =====> Complete the presence check using length

sweet = input("Enter 1 for cake, 2 for jelly, or 3 for fruit: ")


# =====> Complete the lookup check

sweet = input("Enter 1 for cake, 2 for jelly, or 3 for fruit: ")


# =====> Complete the lookup check using 'not in'

choice = input ("Enter YES or NO: ")


# =====> Complete the presence check

Copy and paste the finished code in the space below


# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
colour = ""
teenager = 0
userThere = ""
sweet = 0
choice = ""
sweetOptions = ["1", "2", "3"]

# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
colour = input ("What colour is grass, normally? ")
if (len(colour) > 7 or len(colour) < 2):
print ("Inavid")

teenager = int (input ("How old is a teenager? "))


if(teenager<0 or teenager>18):
print("Invalid")

userThere = input ("Are you there? ")


if userThere == "":
print("Inavlid")
sweet = input("Enter 1 for cake, 2 for jelly, or 3 for fruit: ")
if sweet in sweetOptions:
print("Valid")
sweet = input("Enter 1 for cake, 2 for jelly, or 3 for fruit: ")
if sweet not in sweetOptions:
print("Invalid")

choice = input ("Enter YES or NO: ")


if choice != "YES" and choice != "NO":
print("Invalid")

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)

Copy and paste the finished code in the space below


# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
theChoice = ""
theOptions = ["R", "B", "G", "Q"]
theMenu = ("Choose a colour from the menu:" +
"\nR Red" +
"\nG Green" +
"\nB Blue" +
"\nQ Quit")
# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
print (theMenu)
theChoice = input ("Choose a colour: ")

while theChoice not in theOptions:


print (theMenu)
theChoice = input ("Choice: ")

print ("You chose " + theChoice)

You might also like