Python字符串,列表(密码检查器)

2024-09-27 09:26:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图找出我的代码有哪些地方做错了,哪些地方不能正常工作。即使我在代码中使用小写、大写或数字,它仍然会打印出声明,说我不需要。请帮助!在

# function to check if the password is a valid one 
def validatePassword(userPassword):

    # if the user has a lowercase character, then the password
    # meets validation requirements
    lowercase = "abcdefghijklmnopqrstuvwxyz"
    if lowercase in userPassword:
        hasLowercase = True
    else:
        hasLowercase = False
        print("A lowercase letter is required. ")

    # if the user has an uppercase character, then the password
    # meets validation requirements
    uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    if uppercase in userPassword:
        hasUppercase = True
    else:
        hasUppercase = False
        print("An uppercase letter is required. ")

    # if the user has a digit, then the password
    # meets validation requirements
    digits = "1234567890"
    if digits in userPassword:
        hasDigits = True
    else:
        hasDigits = False
        print("A digit is required. ")

    # if user enters a space, then the password
    # does NOT meet validation requirements
    whitespace = " "
    if whitespace in userPassword:
        hasWhitespace = True
        print("Password must not contain a space.")
    else:
        hasWhitespace = False

    # the user has successfully met all validation requirements
    if ((hasLowercase == True) and (hasUppercase == True) and (hasDigits == True) and (hasWhitespace == False)):
        return True

# the user is inputting passwords     
def main():
    print("Please input passwords one-by-one for validation. ")
    print("Enter END after you've entered your last password. ")
    passwordList = [""]
    while ("END" not in passwordList):
        passwordList.append(str(input("")))
    del passwordList[-1]
    for x in range(1,(len(passwordList))):
        userPassword = passwordList[x]

        # user has come up with a good password
        if (validatePassword(userPassword) == True):
            print("Password " + userPassword + " is good ")

        # user has come up with a bad password
        else:
            print("Password " + userPassword + " is bad ")

main ()

Tags: theinfalsetrueifispasswordelse
2条回答

当您说if '123' in s时,您需要检查整个123字符串是否是子字符串。不是每个角色。在

将其更改为:

if any((c in s) for c in '123')

你的代码会起作用的。 注意,我建议您查看字符串方法upper和{},并查看模块re(正则表达式)。这将帮助您为这个案例编写更好的代码。在

编辑:

在具体情况下,请使用:

^{pr2}$

这个对你有用吗?在

import getpass
import string

# function to check if the password is a valid one 
def validatePassword(userPassword):

    # if the user has a lowercase character, then the password
    # meets validation requirements
    lowercase = list(string.ascii_lowercase)
    hasLowercase = True
    if not any([True if i in lowercase else False for i in userPassword]):
        print("A lowercase letter is required. ")
        hasLowercase = False


    ## if the user has an uppercase character, then the password
    ## meets validation requirements
    uppercase = list(string.ascii_uppercase)
    hasUppercase = True
    if not any([True if i in uppercase else False for i in userPassword]):
        print("An uppercase letter is required. ")
        hasUppercase = False

    ## if the user has a digit, then the password
    ## meets validation requirements
    digits = [str(i) for i in range(10)]
    hasDigits = True
    if not any([True if i in digits else False for i in userPassword]):
        print("A digit is required. ")
        hasDigits = False

    ## if user enters a space, then the password
    ## does NOT meet validation requirements
    whitespace = " "
    if whitespace in userPassword:
        hasWhitespace = True
        print("Password must not contain a space.")
    else:
        hasWhitespace = False

    ## the user has successfully met all validation requirements
    if ((hasLowercase == True) and (hasUppercase == True) and (hasDigits == True) and (hasWhitespace == False)):
        return True
    else:
        return False

if __name__ == '__main__':
    print ("\nPlease input passwords one-by-one for validation\n(Type END after you've entered your last password): ")
    passwordList = []

    while True:
        current_password = getpass.getpass()   # so user password doesn't display on screen, for security reasons. Use next line if you don't want this function.
        #current_password = raw_input()

        if "END" in current_password:
            break

        if current_password:
            passwordList.append(str(current_password))

    for i, userPassword in enumerate(passwordList):
        if validatePassword(userPassword):
            result = "\nPassword " + str(i) + " is good "   # change i to userPassword if you want to print password to screen, NOT RECOMMENDED
        else:
            result = "\nPassword " + str(i) + " is bad "

        print(result)

相关问题 更多 >

    热门问题