Python - 检查密码(弱/强/非常强)

时间:2014-11-28 19:11:54

标签: python python-3.x

我开始使用python语言,我想尝试一下。

inputpass = input("Choose a password?")


def passcheck(password):
    x = 0
    low = False
    cap = False
    hasnum = False
    while x < len(password):
        letter = password[x]
        if letter == letter.lower() and not letter.isnumeric():
            low = True
        elif letter == letter.upper():
            cap = True
        if letter.isnumeric():
            hasnum = True
        else:
            cap = False
            low = False
            hasnum = False
        x += 1
    if cap and low:
        print("You password is strong!")
    elif cap and not low or low and not cap:
        print("You password is weak!")
    elif cap and low and hasnum:
        print("You password is Very Strong!")
    else:
        print("You password is weak!")


passcheck(inputpass)

这是一个检查输入并说明它是弱还是强但我做错了什么的脚本(我知道re模块但是我想坚持这一点直到我能做到这一点没有任何错误。

2 个答案:

答案 0 :(得分:3)

在字符串的每个字符上处理测试的更简短且更易读的方法:

cap = any(chr.isupper() for chr in password)
low = any(chr.islower() for chr in password)
hasnum = any(chr.isnumeric() for chr in password)

答案 1 :(得分:0)

检查一下:

>>> import string
>>> def strong(s):
...     if len(list(set(s)&set(string.ascii_lowercase)))>0 and len(list(set(s)&set(string.ascii_uppercase)))>0 and len(list(set(s)&set(string.digits)))>0 and len(list(set(s)&set(string.punctuation)))>0:
...         return "Strong"
...     else: return "Weak"
... 
>>> strong("Hello?baby1")       # all good
'Strong' 
>>> strong("Hello?baby")        # no mumeric
'Weak' 
>>> strong("?baby")             # no uppercase, no  numeric
'Weak'
>>> strong("ljvjdl48?H")        # all good
'Strong'
>>> strong("hello how are you") # no numeric,no uppercase, no punctuation
'Weak' 
相关问题