繁体   English   中英

Python 密码检查器不正确 Output

[英]Python Password Checker Incorrect Output

我正在尝试在 password_checker.py 中实现 function _isValid() 如果给定的密码字符串满足以下要求则返回 True,否则返回 False:

  • 至少八个字符长
  • 至少包含一位数字 (0-9)
  • 至少包含一个大写字母
  • 至少包含一个小写字母
  • 至少包含一个既不是字母也不是数字的字符

这是我的代码:

import stdio
import sys


# Entry point
def main():
    pwd = sys.argv[1]
    stdio.writeln(_isValid(pwd))


# Returns True if pwd is a valid password, and False otherwise.
def _isValid(pwd):
    check1 = False  # length flag
    check2 = False  # digit flag
    check3 = False  # upper case flag
    check4 = False  # lower case flag
    check5 = False  # alphanumeric flag

    # If pwd is long enough, set corresponding flag to True.
    if len(pwd) >= 8:
        check1 = True

    for c in pwd:
        # For each character c in pwd...

        if c.isdigit():
            # If c is a digit, set corresponding flag to True.
            check2 = True
        elif c.isupper():
            # If c is in upper case, set corresponding flag to True.
            check3 = True
        elif c.islower():
            # If c is in lower case, set corresponding flag to True.
            check4 = True
        elif c.isalnum():
            # If c is not alphanumeric, set corresponding flag to True.
            check5 = True

    # Return True if all flags are True, and False otherwise.
    if check1 and check2 and check3 and check4 and check5:
        return True
    else:
        return False


if __name__ == '__main__':
    main()

我没有得到正确的 output 并且似乎无法弄清楚为什么

您在自己的评论中有答案:

elif c.isalnum():
   # If c is not alphanumeric, set corresponding flag to True.

因此这应该是:

elif not c.isalnum():

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM