170 likes | 294 Views
CMSC 201 – Lab 6. Validation. Overview. Objectives for today's lab: Obtain experience with validation techniques in Python using conditionals, while loops, and try/except blocks. Create a program that uses validation in order to prevent a user from creating an invalid password and pin number.
E N D
CMSC 201 – Lab 6 Validation
Overview • Objectives for today's lab: • Obtain experience with validation techniques in Python using conditionals, while loops, and try/except blocks. • Create a program that uses validation in order to prevent a user from creating an invalid password and pin number.
Program Outline • Your program will be divided into two parts: • The first part will create a password that follows certain guidelines. • The second part will be the same, but instead, making a pin number using a different technique.
Step 0: Setup • The first step is to create a lab6 file in your cs201/labs/lab6 directory From your home directory: cd 201/labs/lab6 emacs lab6.py &
Your Password Rules • Must not include “!”, “.”, or “?” • Must be at least 5 characters in length • At least one character must be a number • Program must re-prompt after invalid input
Step 1: Searching • The first step is to check for the unwanted characters “!”, “.”, and “?” • You can use the “in” keyword to simply search a compatible variable for a given criteria. • Create a check that will prevent the unwanted characters and display an appropriate error message. >>> nameList = ["john", "casey"] >>> "john" in nameList True >>> “u” in “paul” True
Step 2: Finding length • The next step is to make sure the input is longer than 5 characters. • You can use the len() method. • Create a check for this and have it print an appropriate error message. >>> len("john") 4 >>> testStr = "validation" >>> len(testStr) 10
Step 3: Manually Seaching • The last step is to check for the number. • You can loop through the string character and do a comparison one character at a time. • Use ASCII values • Create the check and have it print an appropriate error message. • Look at last two labs for a reminder on using loops in this manner.
Step 4: Continuous Prompt • Because you want to prompt the user indefinitely, for loops are not good enough here. • Put your previous code in a while loop that will run until you have proper input. • Display an appropriate success message. validInput = False while(?): (Previous Code) if(?): // if you have valid input validInput = True
Example Please enter a password: !.? Error: Cannot have “!” Error: Cannot have “.” Error: Cannot have “?” Error: Less than 5 characters Error: Must have at least 1 number Please enter a password: password1 Password created!
Your Pin Number Rules • Must cast the input immediately as an integer. • Must use try/except block to handle input type errors. • Must be at most 5 characters in length • Program must re-prompt after invalid input
Step 5: Try/except block • This coding tool allows you to handle specific error messages that may arise. • Since your input will be cast into an integer, the program should handle this without crashing. • Surround your input line with a try/except block. • In the place of the “????” is where you state what error you want to handle. try: (Prompt) except ????:
Step 5: Try/except block >>> int(input("Enter a number: ")) Enter a number: hello Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'hello' • In our case, we want to handle “ValueError”. • Under the except is where have your error message. try: (Prompt) except ValueError:
Step 6 & 7: Finishing up • Once again, do a check on the length of the input. • Since the input is an integer now, an extra step is required. • Finally, put this second half of the program in a separate while loop to re-prompt.
Example Please enter a pin number: validation Error: Not a number Error: More than 5 characters Please enter a pin number: 1234 Pin number created!
Psuedocode Loop until valid input Get input Check for ! Check for . Check for ? Check length of input Loop through input Check for number ASCII values Check for valid input Loop until valid input try Get input except Check length of input
Example Please enter a password: !.? Error: Cannot have “!” Error: Cannot have “.” Error: Cannot have “?” Error: Less than 5 characters Error: Must have at least 1 number Please enter a password: password1 Password created! Please enter a pin number: validation Error: Not a number Error: More than 5 characters Please enter a pin number: 1234 Pin number created!