Python检测密码是否符合要求,大小写字母数字长度等密码必须包含超过8个字符,且不超过16个字符。密码必须包含至少一个数字、一个大写字母和一个小写字母。密码必须包含多个非字母数字字符。

要使密码更强大,它需要满足以下四个规则:

  1. . 密码必须包含超过8个字符,且不超过16个字符。“

提示:查找密码字符串的长度,您应该使用len()方法。

  1. 密码必须包含至少一个数字、一个大写字母和一个小写字母。
  2. 密码必须包含多个非字母数字字符。

第一种方式是使用函数

  1. if len(a) > 16 or len(a) < 8:
        print(a + "is not of valid length " + str(len(a)))
        isOk = 0
    else:
        print(a + "is of valid length " + str(len(a)))
    print("Check rule 2")
    # Set the function
    lowercases = 0
    uppercases = 0
    digits = 0
    alpha = 0
    # Find, detect output upper and lower case alphanumeric
    for char in a:
        if char.islower():
            lowercases += 1
        elif char.isupper():
            uppercases += 1
        elif char.isdigit():
            digits += 1
    print("Number of lowercase is: " + str(lowercases))
    print("Number of uppercase is: " + str(uppercases))
    print("Number of digit is: " + str(digits))
    if lowercases == 0 or uppercases == 0 or digits == 0:
        print(a + " does not contain enough of each type of character")
        isOk = 0
    else:
        print(a + " contain enough of each type of character")
    print("check rule 3")
    # Detecting special characters
    if alpha != 0:
        print(a + " contain non-alpha-numeric")
    else:
        print(a + " does not contain non-alphanumeric")
        isOk = 0
    print("Check for all the rules")
    if isOk:
        print("Overall " + a + " isvalid")

    第二种最简单的方法

  2. def password_check(passwd):
        SpecialSym = ['$', '@', '#', '%', '@', '!', '&', '=', '+', '_', '-', '$', '/', ]
        val = True
    # check the length
        if len(passwd) < 8:
            print('length should be at > 8')
            val = False
        else:
            print('length is than 8 ')
    
        if len(passwd) > 16:
            print('length should be not be least than 16')
            val = False
        else:
            print('length is least than 16')
    # check the number
        if not any(char.isdigit() for char in passwd):
            print('Password should have at least one numeral')
            val = False
        else:
            print('password have least one numeral')
    
    # check the uppercase
        if not any(char.isupper() for char in passwd):
            print('Password should have at least one uppercase letter')
            val = False
        else:
            print('Password have at least one uppercase letter')
    # check the lowercase
        if not any(char.islower() for char in passwd):
            print('Password should have at least one lowercase letter')
            val = False
        else:
            print('Password have least one lowercase letter')
    # check the Special characters
        if not any(char in SpecialSym for char in passwd):
            print('Password should have at least one of the symbols $@#')
            val = False
        else:
            print('Password have least one of the symbols $@#')
        if val:
            return val
    
    
    def main():
    
        if password_check(password):
    
            print('Password is valid')
        else:
            print("Invalid Password !!")
    
    
    # Driver Code
    if __name__ == '__main__':
        password = input('Enter your password: ')
        main()
    

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年11月3日
下一篇 2023年11月3日

相关推荐