首页 > 试题广场 >

密码强度等级

[编程题]密码强度等级
  • 热度指数:133894 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
密码按如下规则进行计分,并根据不同的得分为密码进行安全等级划分。

一、密码长度:
5 分: 小于等于4 个字符
10 分: 5 到7 字符
25 分: 大于等于8 个字符

二、字母:
0 分: 没有字母
10 分: 密码里的字母全都是小(大)写字母
20 分: 密码里的字母符合”大小写混合“

三、数字:
0 分: 没有数字
10 分: 1 个数字
20 分: 大于1 个数字

四、符号:
0 分: 没有符号
10 分: 1 个符号
25 分: 大于1 个符号

五、奖励(只能选符合最多的那一种奖励):
2 分: 字母和数字
3 分: 字母、数字和符号
5 分: 大小写字母、数字和符号

最后的评分标准:
>= 90: 非常安全
>= 80: 安全(Secure)
>= 70: 非常强
>= 60: 强(Strong)
>= 50: 一般(Average)
>= 25: 弱(Weak)
>= 0:  非常弱(Very_Weak)

对应输出为:

VERY_SECURE
SECURE
VERY_STRONG
STRONG
AVERAGE
WEAK
VERY_WEAK

请根据输入的密码字符串,进行安全评定。

注:
字母:a-z, A-Z
数字:0-9
符号包含如下: (ASCII码表可以在UltraEdit的菜单view->ASCII Table查看)
!"#$%&'()*+,-./     (ASCII码:0x21~0x2F)
:;<=>?@             (ASCII码:0x3A~0x40)
[\]^_`              (ASCII码:0x5B~0x60)
{|}~                (ASCII码:0x7B~0x7E)

提示:
1 <= 字符串的长度<= 300

输入描述:

输入一个string的密码



输出描述:

输出密码等级

示例1

输入

38$@NoNoN

输出

VERY_SECURE

说明

样例的密码长度大于等于8个字符,得25分;大小写字母都有所以得20分;有两个数字,所以得20分;包含大于1符号,所以得25分;由于该密码包含大小写字母、数字和符号,所以奖励部分得5分,经统计得该密码的密码强度为25+20+20+25+5=95分。
         
示例2

输入

Jl)M:+

输出

AVERAGE

说明

示例2的密码强度为10+20+0+25+0=55分。        
while True:#一堆if elif 就完了-。-,善用if 和elif 并列时只运行一个就行了省的打范围
    try:
        a="qwertyuiopasdfghjklzxcvbnm0123456789"
        mark = 0
        ch =0
        nu=0
        nucount=0
        lower=0
        upper=0
        mar=0
        marcount=0
        str1=input()
        if 0<len(str1)<=4:
            mark = 5
        elif 5<=len(str1)<=7:
            mark = 10
        elif len(str1) >= 8:
            mark = 25
        for i in range(len(str1)):
            if str1[i].isalpha() == True:#判断有没有字母
                ch=1
                if str1[i].islower() == True:#有没有小写
                    lower = 1
                if str1[i].isupper() == True:#有没有大写
                    upper = 1
            if str1[i].isdigit() == True:#有没有数字
                nu=1
                nucount=nucount+1#一共几个数字
            if str1[i].lower() not in a: #有没有字符
                mar=1
                marcount=marcount+1#一共几个字符
        if upper == 1 and lower == 1 and nu==1 and mar==1: #满足奖励五分的
            mark = mark + 5
        elif ch==1 and nu==1 and mar==1:#满足奖励三分的
            mark = mark +3
        elif nu==1 and ch==1:#满足奖励2分的
            mark = mark +2
        if upper == 1 and lower == 1:#满足大小写的
            mark = mark +20
        elif ch == 1:#满足有字母的
            mark = mark +10
        if nucount >1:#满足多个数字的
            mark = mark + 20
        elif nucount == 1:#满足一个数字的
            mark = mark + 10
        if marcount >1:#满足多个字符的
            mark = mark + 25
        elif marcount == 1:#满足一个字符的
            mark = mark + 10
        if mark >= 90:
            str2 = "VERY_SECURE"
        elif mark >= 80:
            str2 = "SECURE"
        elif mark >= 70:
            str2 = "VERY_STRONG"
        elif mark >= 60:
            str2 = "STRONG"
        elif mark >= 50:
            str2 = "AVERAGE"
        elif mark >= 25:
            str2 = "WEAK"
        elif mark >= 0:
            str2 = "VERY_WEAK"
        print(str2)
    except:
        break

发表于 2021-07-02 18:52:53 回复(0)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re


class Password_Security:
    def __init__(self):
        self.Password = input()
        self.score = 0
        self.__include_letter0 = False
        self.__include_letter1 = False
        self.__include_symbol = False
        self.__include_number = False

    # 根据密码长度得分
    def lenght(self):
        lens = len(self.Password)
        if lens <= 4:
            self.score += 5
        elif 4 < lens <= 7:
            self.score += 10
        elif lens >= 8:
            self.score += 25
        else:
            pass

    # 根据字母得分
    def letter(self):
        my_re = re.compile(r'[A-Za-z]', re.S)
        # 判读存在字母
        if len(my_re.findall(self.Password)):
            # 存在的字母都是大写字母
            if self.Password.isupper():
                self.score += 10
                self.__include_letter0 = True
            # 存在的字母都是小写字母
            elif self.Password.islower():
                self.score += 10
                self.__include_letter1 = True
            else:
                self.score += 20
                self.__include_letter1 = True
                self.__include_letter0 = True
        # 不存在字母
        else:
            self.score += 0

    # 根据数字得分
    def number(self):
        my_re = re.compile(r'\d+')
        num = ''
        for i in my_re.findall(self.Password):
            num += i
        # 判读是否存在数字
        if num:
            self.__include_number = True
            if len(num) == 1:
                self.score += 10
            else:
                self.score += 20
        else:
            self.score += 0

    # 根据符号得分
    def symbol(self):
        # 判断是否存在符号
        my_re = re.compile(r"[^A-Za-z0-9]")
        str0 = ''
        for i in my_re.findall(self.Password):
            str0 += i
        if str0:
            self.__include_symbol = True
            if len(str0) == 1:
                self.score += 10
            else:
                self.score += 25
        else:
            self.score += 0
        # 根据奖励得分

    def reward(self):
        if self.__include_letter0 and self.__include_letter1 and \
                self.__include_symbol and self.__include_number:
            self.score += 5
        elif (self.__include_symbol and self.__include_number) and \
                (self.__include_letter0&nbs***bsp;self.__include_letter1):
            self.score += 3
        elif (self.__include_number and self.__include_symbol==False) and \
                (self.__include_letter0&nbs***bsp;self.__include_letter1):
            self.score += 2
        else:
            pass

    # 最终输出
    def finish_print(self):
        self.lenght()
        self.letter()
        self.number()
        self.symbol()
        self.reward()
        if self.score >= 90:
            print('VERY_SECURE')
        elif self.score >= 80:
            print('SECURE')
        elif self.score >= 70:
            print('VERY_STRONG')
        elif self.score >= 60:
            print('STRONG')
        elif self.score >= 50:
            print('AVERAGE')
        elif self.score >= 25:
            print('WEAK')
        elif self.score >= 0:
            print('VERY_WEAK')
        else:
            pass


while True:
    try:
        test = Password_Security()
        test.finish_print()
    except:
        break


编辑于 2021-06-13 15:54:18 回复(0)
import sys


def func(s):
    score = 0
    # 1. 长度判断
    if len(s) <= 4:
        score += 5
    elif len(s) >= 8:
        score += 25
    else:
        score += 10
        
    # 数字个数
    has_num = 0
    # 字母个数,小写,大写
    has_xxzm = 0
    has_dxzm = 0
    # 符合个数
    has_fh = 0
    for c in s:
        if c.isdigit():
            has_num += 1
        elif c.isupper():
            has_dxzm += 1
        elif c.islower():
            has_xxzm += 1
        elif c in '''$%&'()*+,-./:;<=>?@[\]^_`{|}~''':
            has_fh += 1
    
    # 全都是小写字母或者大写字母
    if (has_xxzm > 0 and has_dxzm == 0)&nbs***bsp;(has_xxzm == 0 and has_dxzm > 0):
        score += 10
    # 大小写字母混合
    elif has_dxzm > 0 and has_xxzm > 0:
        score += 20
    
    # 一个数字
    if has_num == 1:
        score += 10
    # 大于1个数字
    elif has_num > 1:
        score += 20
    
    # 一个符号
    if has_fh == 1:
        score += 10
    elif has_fh > 1:
        score += 25
    
    # 奖励
    # 字母和数字
    if has_xxzm > 0 and has_dxzm > 0 and has_num > 0 and has_fh > 0:
        score += 5
    elif (has_xxzm > 0&nbs***bsp;has_dxzm > 0) and has_num > 0 and has_fh > 0:
        score += 3
    elif (has_xxzm > 0&nbs***bsp;has_dxzm > 0) and has_num > 0:
        score += 2
        
    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')
    elif score >= 0:
        print('VERY_WEAK')

for line in sys.stdin:
    func(line.strip())

发表于 2021-05-03 15:24:53 回复(0)
import re
while True:
    try:
        password = input()
        score = 0
        #length
        '''
        5分: 小于等于4个字符
        10分: 5到7字符
        25分: 大于等于8个字符
        '''
        if len(password) >= 8:
            score += 25
        elif len(password) >= 5:
            score += 10
        elif len(password) <= 4:
            score += 5
        #alphabets
        '''
        0分: 没有字母
        10分: 全都是小(大)写字母
        20分: 大小写混合字母
        '''
        cnt_upper = len(re.findall('[A-Z]', password))
        cnt_lower = len(re.findall('[a-z]', password))
        if password.isupper() == True or password.islower() == True:
            score += 10
        elif cnt_upper > 0 and cnt_lower > 0:
            score += 20
        #numbers
        '''
        0分: 没有数字
        10分: 1个数字
        20分: 大于1个数字
        '''
        cnt_number = len(re.findall('[0-9]', password))
        if cnt_number == 1:
            score += 10
        elif cnt_number > 1:
            score += 20
        #symbols
        '''
        0分: 没有符号
        10分: 1个符号
        25分: 大于1个符号
        '''
        cnt_symbol = len(re.findall('[^0-9A-Za-z]', password))
        if cnt_symbol == 1:
            score += 10
        elif cnt_symbol > 1:
            score += 25
        #variety bonus
        '''
        2分: 字母和数字
        3分: 字母、数字和符号
        5分: 大小写字母、数字和符号
        '''
        if cnt_upper > 0 and cnt_lower > 0 and cnt_number > 0 and cnt_symbol > 0:
            score += 5
        elif (cnt_upper > 0 or cnt_lower > 0) and cnt_number > 0 and cnt_symbol > 0:
            score += 3
        elif (cnt_upper > 0 or cnt_lower > 0) and cnt_number > 0:
            score += 2
        #score to level
        '''
        >= 90: 非常安全
        >= 80: 安全(Secure)
        >= 70: 非常强
        >= 60: 强(Strong)
        >= 50: 一般(Average)
        >= 25: 弱(Weak)
        >= 0:  非常弱
        '''
        level = ''
        if score >= 90:
            level = 'VERY_SECURE'
        elif score >= 80:
            level = 'SECURE'
        elif score >= 70:
            level = 'VERY_STRONG'
        elif score >= 60:
            level = 'STRONG'
        elif score >= 50:
            level = 'AVERAGE'
        elif score >= 25:
            level = 'WEAK'
        elif score >= 0:
            level = 'VERY_WEAK'
        print(level)
    except EOFError:
        break

编辑于 2021-03-27 22:01:23 回复(1)
while True:
    try:
        mima=input()
        L=len(mima)
        s=0
        if L<5:
            s+=5
        elif 5<=L<=7:
            s+=10
        else:
            s+=25

        f=0
        r=0
        q=0
        for each in mima:
            if each.isupper():
                r=1
            if each.islower():
                q=1
        if r==0 and q==0:
            s+=0
        if (r==1 and q==0) or (r==0 and q==1):
            s+=10
        if r==1 and q==1:
            s+=20
            

        a=0
        for each in mima:
            if each in '1234567890':
                a+=1
        if a==1:
            s+=10
        if a>1:
            s+=20

        b=0
        for each in mima:
            if not each.isalnum():
                b+=1
        if b==1:
            s+=10
        if b>1:
            s+=25



        if (r==1 or q==1) and a!=0 and b==0:
            s+=2
        if ((r==1 and q==0) or (r==0 and q==1)) and a!=0 and b!=0:
            s+=3
        if (r==1 and q==1) and a!=0 and b!=0:
            s+=5

        if s>=90:
            print('VERY_SECURE')
        elif s>=80:
            print('SECURE')
        elif s>=70:
            print('VERY_STRONG')
        elif s>=60:
            print('STRONG')
        elif s>=50:
            print('AVERAGE')
        elif s>=25:
            print('WEAK')
        else:
            print('VERY_WEAK')
    except:
        break
发表于 2021-03-18 00:25:45 回复(0)
lines=[]
while True:
    try:
        lines.append(input())
    except:
        break
for each in lines:
    str1=each
    score=0
    lenth=len(str1)            #密码长度评分
    if lenth<=4:
        score=score+5
    elif 5<=lenth<=7:
        score=score+10
    else:
        score=score+25
    num=0;alp=0;other=0
    for each in str1:
        if each.isalpha():
            alp=alp+1
        elif each.isdigit():
            num=num+1
        else:
            other=other+1
    if alp==0:
        score=score+0                          #字母评分
    elif str1.islower() or str1.isupper():
        score=score+10
    else:
        score=score+20
    if num==0:
        score=score+0             #数字个数评分
    elif num==1:
        score=score+10
    else:
        score=score+20
    if other==0:
        score=score+0                #字符个数评分
    elif other==1:
        score=score+10
    else:
        score=score+25
    up=0;low=0
    for each in str1:
        if each.isalpha():
            if each.isupper():
                up=up+1
            else:
                low=low+1
    if alp>0 and num>0:
        if other>0:
            if up>0 and low>0:
                score=score+5
            else:
                score=score+3
        else:
            score=score+2
    if score>=90:
        print('VERY_SECURE')
    elif 90>score>=80:
        print('SECURE')
    elif 80>score>=70:
        print('VERY_STRONG')
    elif 70>score>=60:
        print('STRONG')
    elif 60>score>=50:
        print('AVERAGE')
    elif 50>score>=25:
        print('WEAK')
    else:
        print('VERY_WEAK')

发表于 2021-03-08 11:20:37 回复(0)
有遇到py3报warning:cannot find your CPU L2 size in...的吗?如何解决
发表于 2021-02-19 19:57:25 回复(0)
# 思路:将统计情况放主函数中一起写,避免分开写时多次遍历密码字符串。

while True:
    try:
        password = input().strip()
        score = 0  # 初始化得分为0
        # 长度得分
        if len(password) <= 4:
            score += 5
        elif 5 <= len(password) <= 7:
            score += 10
        else:
            score += 25
        # 字母、数字、符号统计(是否出现过、出现的个数)
        alpha, Alpha, digit, digit_num, symbol, symbol_num = 0, 0, 0, 0, 0, 0
        for ch in password:
            if ch.islower():
                alpha = 1
            elif ch.isupper():
                Alpha = 1
            elif ch.isdigit():
                digit = 1
                digit_num += 1
            else:
                symbol = 1
                symbol_num += 1
        # 字母得分
        if (alpha and not Alpha) or (Alpha and not alpha):
            score += 10
        elif alpha and Alpha:
            score += 20
        # 数字得分
        if digit_num == 1:
            score += 10
        elif digit_num > 1:
            score += 20
        # 符号得分
        if symbol_num == 1:
            score += 10
        elif symbol_num > 1:
            score += 25
        # 奖励得分
        # 此写法错误,本该属于第三种加分的情况被第二种加分情况拦截住了,加成了第二种情况的分数。
        # if (alpha or Alpha) and digit and not symbol:
        #     score += 2
        # elif (alpha or Alpha) and digit and symbol:
        #     score += 3
        # elif alpha and Alpha and digit and symbol:
        #     score += 5
        if alpha and Alpha and digit and symbol:
            score += 5
        elif (alpha or Alpha) and digit and symbol:
            score += 3
        elif (alpha or Alpha) and digit:
            score += 2
        # 分数等级
        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')
    except:
        break

编辑于 2020-12-28 18:12:18 回复(0)
import re
import sys
import traceback


others = r"[!\"#\$%&\'\(\)\*\+,-\./:;<=>\?@\[\\\]\^_`\{\|\}~]"


def cal_len(p):
    l = len(p)
    return 5 if l<5 else 10 if l<8 else 25


def cal_c(p):
    lowers = re.findall(r"[a-z]", p)
    uppers = re.findall(r"[A-Z]", p)
    return 0 if not lowers and not uppers else 20 if lowers and uppers else 10


def cal_n(p):
    nums = re.findall(r"[0-9]", p)
    return 0 if not nums else 10 if len(nums)==1 else 20


def cal_o(p):
    l = re.findall(others, p)
    return 0 if not l else 10 if len(l)==1 else 25


def award(p):
    if cal_n(p):
        if cal_c(p):
            if cal_o(p):
                if cal_c(p) == 20:
                    return 5
                return 3
            return 2
    return 0


def cal(p):
    return cal_len(p) + cal_c(p) + cal_n(p) + cal_o(p) + award(p)


def judge(p):
    score = cal(p)
    return ("VERY_SECURE" if score >= 90
            else "SECURE" if score >= 80
            else "VERY_STRONG" if score >= 70
            else "STRONG" if score >= 60
            else "AVERAGE" if score >= 50
            else "WEAK" if score >= 25
            else "VERY_WEAK"
    )


try:
    for p in sys.stdin:
        print(judge(p.strip()))
except Exception as e:
    print(traceback.format_exc())

发表于 2020-12-13 01:38:02 回复(0)
while True:
    try:
        s = input()
        password = []
        password.extend(s)
        p1,p2,p3,p4 = [],[],[],[]

        for i in password:
            if i.isupper():
                p1.append(i)
            elif i.islower():
                p2.append(i)
            elif i.isdigit():
                p3.append(i)
            else:
                p4.append(i)

        score = []
        if len(s) <= 4:         
            score.append(5)
        elif 5 <= len(s) <= 7:
            score.append(10)
        else:
            score.append(25)

        if p1&nbs***bsp;p2:        
            if len(p1) == 0&nbs***bsp;len(p2) == 0:
                score.append(10)
            else:
                score.append(20)

        if len(p3) == 1:        
            score.append(10)
        elif len(p3) > 1:
            score.append(20)

        if len(p4) == 1:       
            score.append(10)
        elif len(p4) > 1:
            score.append(25)

        if p1 and p2 and p3 and p4:
            score.append(5)
        elif (p1&nbs***bsp;p2) and p3 and p4:
            score.append(3)
        elif (p1&nbs***bsp;p2) and  p3:      
            score.append(2)

        result = sum(score)
        if result >= 90:
            print('VERY_SECURE')
        elif result >= 80:
            print('SECURE')
        elif result >= 70:
            print('VERY_STRONG')
        elif result >= 60:
            print('STRONG')
        elif result >= 50:
            print('AVERAGE')
        elif result >= 25:
            print('WEAK')
        elif result >= 0:
            print('VERY_WEAK')
    except:break


编辑于 2020-12-01 14:49:45 回复(0)
def changdu(p):
    if len(p) <= 4:
        return 5
    elif 5 <= len(p) <= 7:
        return 10
    elif len(p) >= 8:
        return 25
def zimu(p):
    n = 0
    for i in p:
        if i.isalpha():
            n+=1
    if n == 0:
        return 0
    elif p.upper() == p&nbs***bsp;p.lower() == p:
        return 10
    else:
        return 20

def shuzi(p):
    n = 0
    for i in p:
        if i.isdigit():
            n+=1
    if n==0:
        return 0
    elif n==1:
        return 10
    else:
        return 20

def fuhao(p):
    n =0
    for i in p:
        if not i.isdigit() and not i.isalpha():
            n+=1
    if n == 0:
        return 0
    elif n== 1:
        return 10
    else:
        return 25
def jaingli(p):
    a = 0#字母
    b = 0#数字
    c = 0#符号
    for i in p:
        if p.isalpha():
            a+=1
        elif p.isdigit():
            b+=1
        else:
            c+=1
    if a!=0 and b!=0 and c==0:
        return 2
    elif a!=0 and b!=0 and c!=0:
        return 3
    else:
        return 5
while True:
    try:
        p = input()
        s_changdu = changdu(p)
        s_zimu = zimu(p)
        s_shuzi = shuzi(p)
        s_fuhao = fuhao(p)
        s_jiangli = jaingli(p)
        s_sum = s_changdu + s_zimu + s_shuzi + s_fuhao +s_jiangli
        if s_sum>=90:
            print("VERY_SECURE")
        elif s_sum>=80:
            print("SECURE")
        elif s_sum>=70:
            print("VERY_STRONG")
        elif s_sum>=60:
            print("STRONG")
        elif s_sum>=50:
            print("AVERAGE")
        elif s_sum>=25:
            print("WEAK")
        else:
            print("VERY_WEAK")
    except:
        break


编辑于 2020-11-20 12:13:19 回复(0)
# 2020年11月14日23:28:18
def get_scord(password):
    scord = 0
#   验证密码长度
    length = len(password)
#   长度得分
    if length <= 4:
        scord += 5
    elif length <= 7:
        scord += 10
    else:
        scord += 25
#   验证密码字母、数字、符号个数
    lower = 0
    captial = 0
    number = 0
    symbol = 0
    for i in range(length):
        if "a"<=password[i]<="z":
            lower += 1
        elif "A"<=password[i]<="Z":
            captial += 1
        elif "0"<=password[i]<="9":
            number += 1
        else:
            symbol += 1
#   字母得分
    if lower>0 and captial>0:
        scord += 20
    elif lower == 0 and captial == 0:
        scord += 0
    else:
        scord += 10
    
#   数字得分
    if number == 0:
        scord += 0
    elif number == 1:
        scord += 10
    else:
        scord += 20
#   符号得分
    if symbol == 0:
        scord += 0
    elif symbol == 1:
        scord += 10
    else:
        scord += 25
#   奖励得分
    if lower>0 and captial>0 and number>0 and symbol>0:
        scord += 5
    elif (lower>0&nbs***bsp;captial>0) and number>0 and symbol>0:
        scord += 3
    elif (lower>0&nbs***bsp;captial>0) and number>0 and symbol==0:
        scord += 2
    return scord

while True:
    try:
        password = input()
#       获取分数
        scord = get_scord(password)
#       根据分数判断密码等级
        if scord>=90:
            print("VERY_SECURE")
        elif scord>=80:
            print("SECURE")
        elif scord>=70:
            print("VERY_STRONG")
        elif scord>=60:
            print("STRONG")
        elif scord>=50:
            print("AVERAGE")
        elif scord>=25:
            print("WEAK")
        elif scord>=0:
            print("VERY_WEAK")
    except:
        break
        

发表于 2020-11-15 00:24:11 回复(0)
条理清晰,简单易懂
while True:
    try:
        s = input().strip()
        p_len, low, upper, digit, ch = 0, 0, 0, 0, 0
        flag = 0 # 四位二进制标记是否存在 0000 分别代表 字符 数字 大写 小写
        for i in s:
            p_len += 1
            if i.islower():
                low += 1
                flag |= 0b0001
            elif i.isupper():
                upper += 1
                flag |= 0b0010
            elif i.isdigit():
                digit += 1
                flag |= 0b0100
            else:
                ch += 1
                flag |= 0b1000
        score1 = 5 if p_len <= 4 else 10 if 5 <= p_len <= 7 else 25
        score2 = 0 if flag & 0b0011 == 0 else 20 if flag & 0b0011 == 3 else 10
        score3 = 0 if digit == 0 else 10 if digit == 1 else 20
        score4 = 0 if ch == 0 else 10 if ch == 1 else 25
        score5 = 2 if 5 <= flag <= 7 else 3 if 13 <= flag <= 14 else 5 if flag == 15 else 0
        score = score1 + score2 + score3 + score4 +score5
        res = ('VERY_SECURE' if score >= 90 else 'SECURE' if score >= 80 else
               'VERY_STRONG' if score >= 70 else 'STRONG' if score >= 60 else
               'AVERAGE' if score >= 50 else 'WEAK' if score >= 25 else 'VERY_WEAK')
        print(res)
    except:
        break
        


编辑于 2020-10-03 10:23:11 回复(0)
while True:
    try:
        str = input()
        part1 = 0
        part2 = 0
        part3 = 0
        part4 = 0
        part5 = 0
        
        length = len(str)
        if length <= 4:
            part1 = 5
        elif length>=5 and length<=7 :
            part1 = 10
        else:
            part1 = 25
            
        numCount = 0
        signCount = 0
        lowerCount = 0
        upperCount = 0
        alphaCount = 0
        
        for s in str:
            if s.isdigit():
                numCount = numCount+1
            if not s.isdigit() and not s.isalnum():
                signCount = signCount+1
            if s.islower():
                lowerCount = lowerCount+1
            if s.isupper():
                upperCount = upperCount+1
            if s.isalpha():
                alphaCount = alphaCount+1
                
        if lowerCount == 0 and upperCount > 0:
            part2 = 10
        elif lowerCount > 0 and upperCount == 0:
            part2 = 10
        elif lowerCount > 0 and upperCount > 0:
            part2 = 20
                
        if numCount == 1:
            part3 = 10
        elif numCount > 1:
            part3 = 20
            
        if signCount == 1:
            part4 = 10
        elif signCount > 1:
            part4 = 25
            
            
        if part2 == 10 and part3 > 0 and part4 == 0:
            part5 = 2
        elif part2 == 10 and part3 > 0 and part4 > 0:
            part5 = 3
        elif part2 == 20 and part3 > 0 and part4 > 0:
            part5 = 5
            
        sum = part1+part2+part3+part4+part5
        
        if sum >= 90:
            print("VERY_SECURE")
        elif sum >=80 and sum <90:
            print("SECURE")
        elif sum >=70 and sum <80:
            print("VERY_STRONG")
        elif sum >=60 and sum <70:
            print("STRONG")
        elif sum >=50 and sum <60:
            print("AVERAGE")
        elif sum >=25 and sum < 50:
            print("WEAK")
        else:
            print("VERY_WEAK")     
        
    except:
        break
发表于 2020-09-29 19:26:39 回复(0)
参考通过小伙伴的代码
while True:
    try:
        s = input()
        l = len(s)
        p = 0
        d = 0
        lower = 0
        upper = 0
        f = 0
        t = ['0', '0', '0', '0']
        
        # 密码长度判断
        if l < 5:
            p += 5
        elif l < 8:
            p += 10
        else:
            p += 25

        for i in range(0, l):
            if s[i].isdigit(): # 数字判断
                t[0] = '1'
                if d < 20:
                    d += 10
            elif s[i].isupper(): # 大小判断
                t[1] = '1'
                if lower < 10:
                    lower += 10
            elif s[i].islower(): # 小写判断
                t[2] = '1'
                if upper < 10:
                    upper += 10
            else: # 符号判断
                t[3] = '1'
                if f == 10: # 2个即得满25分,2个以上不执行下述判断
                    f = 25
                elif f == 0: # 一个符号
                    f = 10
        p += d + lower + upper + f
        t_s = ''.join(t)
        # 建立奖励的判别标准,采用0, 1来判断十分方便
        if t_s == '1111':
            p += 5
        elif t_s == '1011'&nbs***bsp;t_s == '1101':
            p += 3
        elif t_s[1:2] != '00':
            p += 2

        if p >= 90:
            print('VERY_SECURE')
        elif p >= 80:
            print('SECURE')
        elif p >= 70:
            print('VERY_STRONG')
        elif p >= 60:
            print('STRONG')
        elif p >= 50:
            print('AVERAGE')
        elif p >= 25:
            print('WEAK')
        else:
            print('VERY_WEAK')
    except:
        break


发表于 2020-08-24 15:06:16 回复(0)
import re

def secure_level():
    str1 = input()
    score = 0

    # length
    n = len(str1)
    if n <= 4:
        score += 5
    elif n >= 5 and n <= 7:
        score += 10
    else:
        score += 25

    # three elements
    char_lower = 0
    char_upper = 0
    nums = 0
    signs = 0
    # char
    temp = re.findall('[a-z]', str1)
    char_lower = len(temp)
    if char_lower: score += 10
    temp = re.findall('[A-Z]', str1)
    char_upper = len(temp)
    if char_upper: score += 10

    # number
    temp = re.findall('[0-9]', str1)
    nums = len(temp)
    if nums == 1:
        score += 10
    elif nums >= 2:
        score += 20

    # sign
    signs = n - char_lower - char_upper - nums
    if signs == 1:
        score += 10
    elif signs >= 2:
        score += 25

    # reward
    if char_lower and char_upper and nums and signs:
        score += 5
    elif (char_lower&nbs***bsp;char_upper) and nums and signs:
        score += 3
    elif (char_lower&nbs***bsp;char_upper) and nums:
        score += 2
    
    if score >= 90: return 'VERY_SECURE'
    elif score >= 80: return 'SECURE'
    elif score >= 70: return 'VERY_STRONG'
    elif score >= 60: return 'STRONG'
    elif score >= 50: return 'AVERAGE'
    elif score >= 25: return 'WEAK'
    elif score >= 0: return 'VERY_WEAK'
    
print(secure_level())


有一个用例一直没法通过 →  PKyt&&fmE^`aBC)t$`Jh^^Rxr(EQ&!N(u)^)`
里面明明没有数字为什么输出最后得分会达到90分呢? 

发表于 2020-08-01 13:05:49 回复(0)
while True:
    try:
        key = input()
        password = []
        p1, p2, p3, p4 = [], [], [], []
        length_of_key = len(key)
        for i in range(0, length_of_key):
            if key[i].isupper():
                p1.append(key[i])
            elif key[i].islower():
                p2.append(key[i])
            elif key[i].isdigit():
                p3.append(key[i])
            else:
                p4.append(key[i])
        score = 0
        if len(key) <= 4:
            score += 5
        elif 7 >= len(key) >= 5:
            score += 10
        else:
            score += 25
        if p1&nbs***bsp;p2:
            if len(p1) == 0&nbs***bsp;len(p2) == 0:
                score += 10
            else:
                score += 20
        if len(p3) == 1:
            score += 10
        elif len(p3) > 1:
            score += 20
        if len(p4) == 1:
            score += 10
        elif len(p4) > 1:
            score += 25
        if (p1&nbs***bsp;p2) and p3:
            score += 2
        elif (p1&nbs***bsp;p2) and p3 and p4:
            score += 3
        elif p1 and p2 and p3 and p4:
            score += 5

        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')
        elif score >= 0:
            print('VERY_WEAK')

    except:
        break


编辑于 2020-07-29 23:38:01 回复(0)
题不难,就是分类情况多了些
def get_score(s):
    score, award = 0, 0
    # mark标记各类型个数,分别是小写字母,大写字母,数字,符号个数
    mark = [0] * 4
    score += (len(s)<=4)*5 + (4< len(s) <8)*10+ (len(s)>=8)*25
    for i in s:
        if 'A' <= i <= 'Z':
            mark[0] += 1
        elif 'a' <= i <= 'z':
            mark[1] += 1
        elif '0' <= i <= '9':
            mark[2] += 1
        elif '!' <= i <= '~':
            mark[3] += 1
    score += (mark[0] >= 1)*10 + (mark[1] >= 1)*10
    score += (mark[2] == 1)*10 + (mark[2] > 1)*25
    score += (mark[3] == 1)*10 + (mark[3] > 1)*25
    if mark[0]+mark[1] < 0:
        award = 0
    elif (mark[0]>0 and mark[1]>0) and (mark[2]>0 and mark[3]>0):
        award = 5
    elif mark[0]+mark[1] > 0 and (mark[2]>0 and mark[3]>0):
        award = 3
    elif mark[0]+mark[1] > 0 and mark[2]>0:
        award = 2
        score += award
    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:
        s= input()
        get_score(s)
    except:
        break


编辑于 2020-07-18 20:04:31 回复(0)
while(True):
    try:
        s = input()
        secLength = len(s)
        upperCount = 0
        lowerCount = 0
        numCount = 0
        score = 0
        charCount = 0

        for i in s:
            # 数字个数
            if(i.isdigit()):
                numCount+=1
            elif(i.islower()):
                lowerCount+=1
            elif(i.isupper()):
                upperCount+=1
            else:
                charCount+=1

        if (secLength <= 4):
            score += 5
        elif (5 < secLength <= 7):
            score += 10
        elif (secLength >= 8):
            score += 25

        if (numCount == 0):
            score += 0
        elif (numCount==1):
            score += 10
        elif (numCount >= 1):
            score += 20

        if(upperCount==1 and lowerCount==0):
            score+=0
        elif(upperCount==0 and lowerCount>0) or (upperCount>0 and lowerCount==0):
            score+=10
        elif(upperCount>0 and lowerCount>0):
            score+=20
    except:
        break

    if (charCount == 0):
        score += 0
    elif (charCount==1):
        score += 10
    elif (charCount >= 1):
        score += 25

    if(numCount>0 and (upperCount>0 or lowerCount>0) and charCount==0):
        score+=2
    elif (numCount > 0 and (upperCount > 0 or lowerCount > 0) and charCount > 0):
        score += 3
    elif (numCount > 0 and upperCount > 0 and lowerCount > 0 and charCount == 0):
        score += 5

    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')
发表于 2020-07-11 12:11:55 回复(0)