plapapyjh 2022-09-03 15:58 采纳率: 43.5%
浏览 73
已结题

python初学者,验证密码是否符合要求问题,在写验证这部分时不会了

python初学者,验证密码是否符合要求问题,在写验证这部分时不会了

"""
Please enter a valid password
Your password must be between 5 and 15 characters, and contain:
1 or more uppercase characters
1 or more lowercase characters
1 or more numbers and 1 or more special characters: !@#$%^&*()_-=+`~,./'[]<>?{}|\
"""

MIN_LENGTH = 5
MAX_LENGTH = 15
SPECIAL_CHARS_REQUIRED = False
SPECIAL_CHARACTERS = "!@#$%^&*()_-=+`~,./'[]<>?{}|\\"


def main():
    """Program to get and check a user's password."""
    print("Please enter a valid password")
    print("Your password must be between", MIN_LENGTH, "and", MAX_LENGTH,
          "characters, and contain:")
    print("\t1 or more uppercase characters")
    print("\t1 or more lowercase characters")
    print("\t1 or more numbers")
    if SPECIAL_CHARS_REQUIRED:
        print("\tand 1 or more special characters: ", SPECIAL_CHARACTERS)
    password = input("> ")
    while not is_valid_password(password):
        print("Invalid password!")
        password = input("> ")
    print("Your {}-character password is valid: {}".format(len(password),
                                                           password))


def is_valid_password(password):
    """Determine if the provided password is valid."""
    # TODO: if length is wrong, return False

    count_lower = 0
    count_upper = 0
    count_digit = 0
    count_special = 0
    for char in password:
        # TODO: count each kind of character (use str methods like isdigit)
        pass

    # TODO: if any of the 'normal' counts are zero, return False

    # TODO: if special characters are required, then check the count of those
    # and return False if it's zero

    # if we get here (without returning False), then the password must be valid
    return True


main()

上面在验证密码函数部分,原题已经给了注释,我这部分卡住了,写不出来,麻烦看一下,给一个完整补充好的代码,如果能有详细一点的注释就更好啦,谢谢

  • 写回答

1条回答 默认 最新

  • sinJack 2022-09-03 16:16
    关注
    MIN_LENGTH = 5
    MAX_LENGTH = 15
    SPECIAL_CHARS_REQUIRED = False
    SPECIAL_CHARACTERS = "!@#$%^&*()_-=+`~,./'[]<>?{}|\\"
    def  main():
        """Program to get and check a user's password."""
        print("Please enter a valid password")
        print("Your password must be between", MIN_LENGTH, "and", MAX_LENGTH,
              "characters, and contain:")
        print("\t1 or more uppercase characters")
        print("\t1 or more lowercase characters")
        print("\t1 or more numbers")
        if SPECIAL_CHARS_REQUIRED:
            print("\tand 1 or more special characters: ", SPECIAL_CHARACTERS)
        password = input("> ")
        while not is_valid_password(password):
            print("Invalid password!")
            password = input("> ")
        print("Your {}-character password is valid: {}".format(len(password),
                                                               password))
     
     
    def is_valid_password(password):
        if len(password)<MIN_LENGTH or len(password)>MAX_LENGTH:
            return False
        count_lower = 0
        count_upper = 0
        count_digit = 0
        count_special = 0
        for char in password:
            if char.isdigit():
                count_digit+=1
            elif char.isupper():
                count_upper+=1
            elif char.islower():
                count_lower+=1
            elif char in SPECIAL_CHARACTERS:
                count_special+=1
    
        if count_lower==0 or count_upper==0 or count_digit==0:
            return False
        if SPECIAL_CHARS_REQUIRED and count_special==0:
            return False
        return True
    
    main()
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 9月12日
  • 已采纳回答 9月4日
  • 赞助了问题酬金10元 9月3日
  • 创建了问题 9月3日

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等