SimeonTs

SUPyF2 Functions-Exercise - 06. Password Validator

Oct 8th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. """
  2. Functions - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1728#5
  4.  
  5. SUPyF2 Functions-Exercise - 06. Password Validator
  6.  
  7. Problem:
  8. Write a function that checks if a given password is valid. Password validations are:
  9. • The length should be 6 - 10 characters (inclusive)
  10. • It should consists only letters and digits
  11. • It should have at least 2 digits
  12. If a password is valid print "Password is valid".
  13. If it is NOT valid, for every unfulfilled rule print a message:
  14. • "Password must be between 6 and 10 characters"
  15. • "Password must consist only of letters and digits"
  16. • "Password must have at least 2 digits"
  17.  
  18. Examples:
  19. Input:          Output:
  20.  
  21. logIn       Password must be between 6 and 10 characters
  22.            Password must have at least 2 digits
  23.  
  24. MyPass123   Password is valid
  25.  
  26. Pa$s$s      Password must consist only of letters and digits
  27.            Password must have at least 2 digits
  28. """
  29.  
  30.  
  31. def password_validator(password):
  32.     valid_length = False
  33.     only_letters_and_digits = False
  34.     at_least_2_digits = False
  35.  
  36.     if 6 <= len(password) <= 10:
  37.         valid_length = True
  38.     else:
  39.         print("Password must be between 6 and 10 characters")
  40.     if password.isalnum():
  41.         only_letters_and_digits = True
  42.     else:
  43.         print("Password must consist only of letters and digits")
  44.     if len([digit for digit in password if digit.isdigit()]) >= 2:
  45.         at_least_2_digits = True
  46.     else:
  47.         print("Password must have at least 2 digits")
  48.     if valid_length and only_letters_and_digits and at_least_2_digits:
  49.         print("Password is valid")
  50.  
  51.  
  52. password_validator(input())
Add Comment
Please, Sign In to add comment