-2

我正在开发一个密码检查器,用于检查字符串是否为有效密码。我必须检查是否至少有八个字符,必须只包含字母和数字,最后两个字符必须是数字。

到目前为止,这一切似乎都有效,除了password.isdigit(). 有时密码有效,有时无效。有什么建议么?

# Gets the users password
password = input('Enter a string for password: ')
# Splices the last two characters of the password
lastTwo = password[-2:]

# Checks the password if it is less than 8 characters
while len(password) < 8:
    print('The password you entered is too short.')
    print()
    password = input('Enter a string for password: ')

    # Checks the password if it is composed of letters and numbers
    while password.isalnum() == False:
        print('Your password has special characters not allowed.')
        print()
        password = input('Enter a string for password: ')

    # Checks the spice to verify they are digits
    while lastTwo.isdigit() == False:
        print('Your last two characters of your password must be digits.')
        print()
        password = input('Enter a string for password: ')

print('Your password is valid.')
4

2 回答 2

2

您提供的代码存在一些问题。特别是,您只检查后续规则while len(password) < 8。如果你给它一个长度为 10 的密码,则永远不会检查规则。此外,您不会lastTwo使用每个尝试的新密码来更新

解决此问题的一种方法是将您的多个while语句替换为if...elif..elif...else...包含在整体while语句中的语句,如下所示:

# Gets the users password
password = input('Enter a string for password: ')

while True:
    # Checks the password if it is less than 8 characters
    if len(password) < 8:
        print('The password you entered is too short.')
    # Checks the password if it is composed of letters and numbers
    elif not password.isalnum():
        print('Your password has special characters not allowed.')
    # Checks the spice to verify they are digits
    elif not password[:-2].isdigit():
        print('Your last two characters of your password must be digits.')
    else:
        # we only get here when all rules are True
        break

    print()
    password = input('Enter a string for password: ')

print('Your password is valid.')

这应该按您的预期工作。但是,当我们这样做的时候,为什么不告诉用户他们的密码被破坏的每条规则呢?从 UI 的角度来看,它有助于让用户了解情况。

如果我们在是否满足相关规则的同时存储一条信息消息,我们可以快速计算出所有被违反的规则,如下所示:

valid_password = False

while not valid_password:
    # Get a password
    password = input('\nEnter a string for password: ')
    # applies all checks
    checks = {
        '- end in two digits': password[-2].isdigit(),
        '- not contain any special characters': password.isalnum(),
        '- be over 8 characters long': len(password) > 8
    }
    # if all values in the dictionary are true, the password is valid.
    if all(checks.values()):
        valid_password = True
    # otherwise, return the rules violated
    else:
        print('This password is not valid. Passwords must:\n{}'.format(
            '\n'.join([k for k, v in checks.items() if not v])))

print('Your password is valid.')
于 2017-04-05T17:46:20.063 回答
0

lastTwo你永远不会在你的 while 循环中更新你的值。因此想象一下,如果用户首先输入了密码abc123。那么lastTwo将被计算为23

现在您的代码会发现密码太短并提示用户输入新密码。假设他进入abcdefgh。这现在通过了您的第一次和第二次检查。但是请注意,lastTwo它仍然是23,因此您的第三次检查将错误地通过。

因此,每当您接受新密码或直接检查时,您都应该重新计算 lastTwo 的值,如下所示:

while (password[-2:]).isdigit() == False:

于 2017-04-05T17:34:11.997 回答