题解 | #密码强度等级#
密码强度等级
http://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
def count_char(x):
    count = 0
    for i in range(len(x)):
        if x[i].isalpha():
            count += 1
    return count
def count_digits(x):
    count = 0
    for i in range(len(x)):
        if x[i].isdigit():
            count += 1
    return count
def count_fuhao(x):
    fsm = "!\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~"
    count = 0
    for i in range(len(x)):
        if x[i] in fsm:
            count += 1
    return count
def func(x):
    score = 0
    score += 5 if len(x) <= 4 else 10 if 5 <= len(x) <= 7 else 25
    score += 0 if count_char(x) <= 0 else 10 if (x.islower() or x.isupper()) else 20
    score += 0 if count_digits(x) <= 0 else 10 if count_digits(x) == 1 else 20
    score += 0 if count_fuhao(x) <= 0 else 10 if count_fuhao(x) == 1 else 25
    if count_char(x) > 0 and not x.islower() and count_digits(x) > 0 and count_fuhao(x) > 0:
        score += 5
    elif count_char(x) > 0 and count_digits(x) > 0 and count_fuhao(x) > 0:
        score += 3
    elif count_char(x) > 0 and count_digits(x) > 0:
        score += 2
    else:
        score += 0
    if score >= 90:
        print('VERY_SECURE')
    elif score >= 80:
        print('SECURE')
    elif score >= 70:
        print('VERY_STRONG')
    elif score >= 60:
        print('STRONG')
    elif score >= 50:
        print('AVERAGE')
    elif score >= 25:
        print('WEAK')
    else:
        print('VERY_WEAK')
while True:
    try:
        a = input()
        func(a)
    except:
        break
 查看9道真题和解析
查看9道真题和解析