1 / 17

CMSC 201 – Lab 6

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.

julio
Download Presentation

CMSC 201 – Lab 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 201 – Lab 6 Validation

  2. 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.

  3. 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.

  4. 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 &

  5. 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

  6. 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

  7. 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

  8. 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.

  9. 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

  10. 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!

  11. 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

  12. 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 ????:

  13. 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:

  14. 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.

  15. 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!

  16. 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

  17. 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!

More Related