【问题标题】:str.isdigit() doesn't seem to be working in pythonstr.isdigit() 似乎在 python 中不起作用
【发布时间】:2017-08-31 10:53:41
【问题描述】:

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

到目前为止,除了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.')

【问题讨论】:

  • 哪些输入与您的预期不同?
  • “任何建议” - 寻找真正的问题。在你的代码和/或你的理解中。
  • 您应该在最后一个循环之前拼接最后两个字符的密码。否则它将包含以前密码的最后 2 个字符
  • 第一次输入密码后,您永远不会更新lastTwo - 您需要将其带入while 循环

标签: python password-checker


【解决方案1】:

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

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

# 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.')

【讨论】:

  • 虽然我同意你的观点。我实际上并没有回答(相当糟糕的)问题,是吗?
  • @SiHa 你是绝对正确的 - 编辑添加更直接的“修复”
  • 谢谢。这确实更有意义,而且更简洁。我对 python 和编码还是很陌生,所以我很感激你的帮助。
【解决方案2】:

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

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

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

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

【讨论】:

  • 是的,现在你提到它非常有道理,我有点失望,我没有意识到这一点。谢谢
猜你喜欢
  • 1970-01-01
  • 2011-08-18
  • 2017-12-31
  • 2018-08-11
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
  • 2015-05-20
相关资源
最近更新 更多