当前位置:   article > 正文

Python检测密码是否符合要求,大小写字母数字长度等密码必须包含超过8个字符,且不超过16个字符。密码必须包含至少一个数字、一个大写字母和一个小写字母。密码必须包含多个非字母数字字符。_python用户密码要有大小写个数字,且要超过8位

python用户密码要有大小写个数字,且要超过8位

要使密码更强大,它需要满足以下四个规则:

  1. . 密码必须包含超过8个字符,且不超过16个字符。“

提示:查找密码字符串的长度,您应该使用len()方法。

  1. 密码必须包含至少一个数字、一个大写字母和一个小写字母。
  2. 密码必须包含多个非字母数字字符。

第一种方式是使用函数

    1. if len(a) > 16 or len(a) < 8:
    2. print(a + "is not of valid length " + str(len(a)))
    3. isOk = 0
    4. else:
    5. print(a + "is of valid length " + str(len(a)))
    6. print("Check rule 2")
    7. # Set the function
    8. lowercases = 0
    9. uppercases = 0
    10. digits = 0
    11. alpha = 0
    12. # Find, detect output upper and lower case alphanumeric
    13. for char in a:
    14. if char.islower():
    15. lowercases += 1
    16. elif char.isupper():
    17. uppercases += 1
    18. elif char.isdigit():
    19. digits += 1
    20. print("Number of lowercase is: " + str(lowercases))
    21. print("Number of uppercase is: " + str(uppercases))
    22. print("Number of digit is: " + str(digits))
    23. if lowercases == 0 or uppercases == 0 or digits == 0:
    24. print(a + " does not contain enough of each type of character")
    25. isOk = 0
    26. else:
    27. print(a + " contain enough of each type of character")
    28. print("check rule 3")
    29. # Detecting special characters
    30. if alpha != 0:
    31. print(a + " contain non-alpha-numeric")
    32. else:
    33. print(a + " does not contain non-alphanumeric")
    34. isOk = 0
    35. print("Check for all the rules")
    36. if isOk:
    37. print("Overall " + a + " isvalid")

    第二种最简单的方法

    1. def password_check(passwd):
    2. SpecialSym = ['$', '@', '#', '%', '@', '!', '&', '=', '+', '_', '-', '$', '/', ]
    3. val = True
    4. # check the length
    5. if len(passwd) < 8:
    6. print('length should be at > 8')
    7. val = False
    8. else:
    9. print('length is than 8 ')
    10. if len(passwd) > 16:
    11. print('length should be not be least than 16')
    12. val = False
    13. else:
    14. print('length is least than 16')
    15. # check the number
    16. if not any(char.isdigit() for char in passwd):
    17. print('Password should have at least one numeral')
    18. val = False
    19. else:
    20. print('password have least one numeral')
    21. # check the uppercase
    22. if not any(char.isupper() for char in passwd):
    23. print('Password should have at least one uppercase letter')
    24. val = False
    25. else:
    26. print('Password have at least one uppercase letter')
    27. # check the lowercase
    28. if not any(char.islower() for char in passwd):
    29. print('Password should have at least one lowercase letter')
    30. val = False
    31. else:
    32. print('Password have least one lowercase letter')
    33. # check the Special characters
    34. if not any(char in SpecialSym for char in passwd):
    35. print('Password should have at least one of the symbols $@#')
    36. val = False
    37. else:
    38. print('Password have least one of the symbols $@#')
    39. if val:
    40. return val
    41. def main():
    42. if password_check(password):
    43. print('Password is valid')
    44. else:
    45. print("Invalid Password !!")
    46. # Driver Code
    47. if __name__ == '__main__':
    48. password = input('Enter your password: ')
    49. main()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/556231
推荐阅读
相关标签
  

闽ICP备14008679号