Advertisement
makispaiktis

Password Creation

Apr 28th, 2019 (edited)
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.85 KB | None | 0 0
  1. # 1. Intro
  2. print("*******************************************************************************************************")
  3. print("*******************************************************************************************************")
  4. print("Welcome to PasswordCreation Programme. To create your password, you have to follow the following rules: ")
  5. print("1. Your password length must be at least 8 characters.")
  6. print("2. Your password must contain at least 1 lowerCase letter.")
  7. print("3. Your password must contain at least 1 upperCase letter.")
  8. print("4. Your password must contain at least l number.")
  9. print("5. Your password must not contain 'Spacebar' character.")
  10. print("6. Your password must not contain special characters.")
  11. print()
  12. print("Then, you will have to confirm your password.")
  13.  
  14. print("Please enter your password: ")
  15. # Ask from the  user to give me his string/password
  16. password = input()
  17. lowerAlphabet = "abcdefghijklmnopqrstuvwxyz"
  18. upperAlphabet = lowerAlphabet.upper()
  19. numbers = "1234567890"
  20. # Create 6 basic variables for checking the 4 terms and 1 variable to count how many conditions are satisfied
  21. lowerCounter = 0                         # Condition 2
  22. upperCounter = 0                         # Condition 3
  23. numberCounter = 0                        # Condition 4
  24. spacebarFlag = False                     # Condition 5
  25. specialCharactersFlag = False            # Condition 6
  26. specialCharacters = []                   # Create a list, that will contain all the special characters of password
  27. conditionsSatisfied = True               # Boolean value for checking if the conditions for a valid password are satisfied (Initially, I suppose it's true)
  28.  
  29. # Scan the password and investigate the conditions
  30. length = len(password)
  31. for ch in password:
  32.     # For every character in user's password, I check the 1st condition/rule
  33.     if ch in lowerAlphabet:                     # Ayto shmainei an to ch = character anhkei stin "lexi" lowerAlphabet
  34.        lowerCounter += 1
  35.     elif ch in upperAlphabet:
  36.         upperCounter +=1
  37.     elif ch in numbers:
  38.         numberCounter += 1
  39.     elif ch == ' ':
  40.         spacebarFlag = True
  41.     else:
  42.         specialCharactersFlag = True
  43.         specialCharacters.append(ch)                            # I add the special character "ch" to the appropriate list
  44.  
  45. # So, I have some values on my variables
  46. # I will compare these values with my conditions/rules
  47. # 1st check
  48. if len(password) < 8:
  49.     conditionsSatisfied = False
  50. if lowerCounter == 0:
  51.     conditionsSatisfied = False
  52. if upperCounter == 0:
  53.     conditionsSatisfied = False
  54. if numberCounter == 0:
  55.     conditionsSatisfied = False
  56. if spacebarFlag:
  57.     conditionsSatisfied = False                     # If spacebarFlag == True, that means that password contains a spacebar ---> not a valid password
  58. if specialCharactersFlag:
  59.     conditionsSatisfied = False
  60.  
  61. # All the conditions have been checked. THE RESULTS OF INVESTIGATIONS WILL BE PRINTED ON THE SCREEN BELOW
  62. print()
  63. # IF PASSWORD =  INVALID
  64. if not conditionsSatisfied:
  65.     #print()
  66.     print("Not a valid password. Problems with password: " + password)
  67.     # Here, I will explain in which parts the password has problem
  68.     if len(password) < 8:
  69.         print("* Length = " + str(len(password)))
  70.     if lowerCounter == 0:
  71.         print("* No lowerCase letters in the password.")
  72.     if upperCounter == 0:
  73.         print("* No upperCase letters in the password.")
  74.     if numberCounter == 0:
  75.         print("* No number used in the password.")
  76.     if spacebarFlag:
  77.         print("* Your password contains the 'Spacebar' character.")
  78.     if specialCharactersFlag:
  79.         print("* Your password contains special characters: " + ''.join(map(str, specialCharacters)))
  80.  
  81.  
  82. # IF PASSWORD = VALID
  83. else:
  84.     print("That's a valid password. Please confirm this password. You will have only 3 tries.")
  85.     confirmationTries = 0
  86.     while confirmationTries < 3:                                        # Loop with max number of iterations = max tries = 3
  87.         passwordConfirmation = input("Confirm password here: ")          # Ask from the user the password confirmation
  88.         if password != passwordConfirmation:                             # If passwordConfirmation not equal to password, I increase the confirmationTries variable
  89.             confirmationTries += 1
  90.         else:                                                            # If passwordConfirmation equal to password, WE ARE OKEY!
  91.             print("Brilliant. Your new password is: " + password)
  92.             break
  93.  
  94.  
  95.     if confirmationTries == 3:                                  # If we are in the case of wasting both 3 tries
  96.         print()
  97.         print("Password creation failed (you ran out of tries): ")
  98.         print("You entered the password       : " + password)
  99.         print("But, your last confirmation was: " + passwordConfirmation)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement