如何在python中检测密码字符串中的特殊字符

nkcskrwz  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(515)

我正在制作一个程序,你可以在其中添加密码到数据库。密码应包含一个大写、一个小写、一个数字和一个特殊字符,并且应介于8到30个字符之间。除特殊角色部分外,其余部分正常工作。我在字符串中放入了一个特殊字符,但我的程序仍然告诉我它没有特殊字符。我不确定我做错了什么:

specialCharacters = "~!@#$%^&*_-+=`|\(){}[]:;'<>,.?/"

def checkPassword(password):
    global specialCharacters
    errorString = ""
    if(len(password) < 8 or len(password) > 30):
        errorString = "ERROR: Your password must be between 8 and 30 characters"
        print(errorString)
        return False
    elif(any(char.isalpha() for char in password) == False):
        errorString = ("ERROR: password must contain at least letter")
        print(errorString)
        return False
    elif(any(char.isupper() for char in password) == False):
        errorString = ("ERROR: password must contain at least one uppercase")
        print(errorString)
        return False
    elif(any(char.islower() for char in password) == False):
        errorString = ("ERROR: password must contain at least one lowercase")
        print(errorString)
        return False
    elif(any(char.isdigit() for char in password) == False):
        errorString = ("ERROR: password must contain at least one digit")
        print(errorString)
        return False
    for ch in password:                   /// here is the problem
        if ch not in specialCharacters:
            errorString = "ERROR: password must contain at least one special character"
            print(errorString)
            return False
    else:
        return True

checkPassword("Hellooooo1*") // returns "ERROR: password must contain at least one special character", why?
js5cn81o

js5cn81o1#

首先,您要检查密码中的每个字符是否不在特殊字符中-如果该字符串中没有出现任何字符,则执行if语句并拒绝密码。
要重新执行此操作,不需要显式循环,可以使用all关键字:

...
elif all(x not in specialCharacters for x in password):
    errorString = "ERROR: password must contain at least one special character"
    print(errorString)
    return False
return True
t1qtbnec

t1qtbnec2#

您的代码中有一个bug。检查特殊字符的最后一个块仅检查第一个字符,但在那里失败。在做出判断之前,您需要遍历整个字符串。

specialCharacters = "~!@#$%^&*_-+=`|\(){}[]:;'<>,.?/"

def checkPassword(password):
    global specialCharacters
    errorString = ""
    if(len(password) < 8 or len(password) > 30):
        errorString = "ERROR: Your password must be between 8 and 30 characters"
        print(errorString)
        return False
    elif(any(char.isalpha() for char in password) == False):
        errorString = ("ERROR: password must contain at least letter")
        print(errorString)
        return False
    elif(any(char.isupper() for char in password) == False):
        errorString = ("ERROR: password must contain at least one uppercase")
        print(errorString)
        return False
    elif(any(char.islower() for char in password) == False):
        errorString = ("ERROR: password must contain at least one lowercase")
        print(errorString)
        return False
    elif(any(char.isdigit() for char in password) == False):
        errorString = ("ERROR: password must contain at least one digit")
        print(errorString)
        return False
    else:
        special_found = False
        for ch in password:                   
            if ch in specialCharacters:
                speical_found = True
        return speical_found

print(checkPassword("Hellooooo1*"))

相关问题