Python密码验证器

时间:2017-03-03 20:01:28

标签: python-3.x

我试图制作一个基本程序来检查密码是否符合某些标准。这不会返回任何错误,但会验证密码'消息是否有特殊字符。欢迎任何想法!

import re
begin = str(input("Would you like to check your password strength?"))
while begin.lower() == "yes" or begin.lower() == "y":
    password = str(input("Please enter your password"))
    if len(password)<8:
        print ("Your password needs to be at least 8 characters long")
    elif password.isupper() or password.islower():
        print ("Please and include at least one upper and one lower case letter in your password")
    elif password.isnumeric() or password.isalpha():
        print ("Your password should not be numbers only or letters only but should include a mixture of both")
    elif re.match("£$%^&*()",password) == True:
        print ("Your password should contain at least one special character")
    else:
        print ("Password verified")

1 个答案:

答案 0 :(得分:1)

您对re.match()的使用有几个方面存在缺陷。相反,我会提出一个不同的策略:

elif set("£$%^&*()").isdisjoint(password):

这会将您的特殊字符串转换为set个字符,然后检查密码中是否 disjoint ...即,如果它不共享任何元素密码。如果这是真的,那么您就知道密码中没有出现任何特殊字符,因此可以继续进行。