Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Import Python's system functionality module for the exit function when passwords are wrong
- import sys
- # Import Python's regular expressions library, popularly known as Regex
- import re
- # Prompt the user to enter their userName, the userName input will be type converted to str
- userName = str(input("\n\tPlease input your username: \n\n\t"))
- # Print a welcome message string formated to includ the username
- print("\n\t Welcome: {}\n".format(userName))
- # Prompt the user to enter a passWord, input will be type converted to str for regex manipulation
- passWord = str(input("\n\tPlease enter a password.\n\n\t Type here: "))
- # Test to see whether the user enters a series of comma separated passWords
- # Do this by testing the passWord string for the presence of a comma
- # If there's a comma in the passwords sequence, split up the string by comma
- if "," in passWord:
- # Create a list called passWordsList that will hold the passWords
- passWordsList = passWord.split(",")
- # The passWord is the value in passWordsList[0]
- passWord = passWordsList[0]
- # The invalidPassWordMessage string is to be printed on the screen if the password in invalid
- invalidPasswordMessage = """
- Your password is invalid.The password must have:
- 1. At least 1 letter between [a-z]
- 2. At least 1 number between [0-9]
- 3. At least 1 letter between [A-Z]
- 4. At least 1 character from [$#@]
- 5. Minimum length of transaction password: 6
- 6. Maximum length of transaction password: 12
- """
- # Create a boolean test for the validity of the password, with value False
- passwordTest = False
- while passwordTest:
- # Using the regex library search method test if lower case letters are
- # present in the passWord string, the first arguement of the search method
- # is the pattern and the second arguement is the string to be tested
- if re.search("[a-z]", passWord) == False:
- # If the passWord string does not have lower case characters[a-z]
- # print the invalidPasswordMessage
- print("{}".format(invalidPasswordMessage))
- # Exit the application
- sys.exit()
- elif re.search("[0-9]", passWord) == False:
- # If the passWord string does not have any digit from [0-9]
- # print the invalidPasswordMessage
- print("{}".format(invalidPasswordMessage))
- # Exit the application
- sys.exit()
- elif re.search("[A-Z]", passWord) == False:
- # If the passWord string does not have upper case characters [A-Z]
- # print the invalidPasswordMessage
- print("{}".format(invalidPasswordMessage))
- # Exit the application
- sys.exit()
- elif re.search("[$#@]", passWord) == False:
- # If the passWord string does not have the special characters [$#@]
- # print the invalidPasswordMessage
- print("{}".format(invalidPasswordMessage))
- # Exit the application
- sys.exit()
- elif (len(passWord) < 6 or len(passWord)>12):
- # If the passWord string does not meet length specifications
- # print the invalidPasswordMessage
- print("{}".format(invalidPasswordMessage))
- # Exit the application
- sys.exit()
- else:
- # If all the passWord conditions have been met, print the username and password
- print("\n\t {}, your password is: {}".format(userName, passWord))
- # Exit the application
- sys.exit()
Add Comment
Please, Sign In to add comment