题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
def Score(s): score=0 # 密码长度得分 if len(s)<=4: score += 5 elif 5<=len(s)<=7: score += 10 elif len(s)>=8: score += 25 # 字母得分 LETTER=''.join(i for i in s if 65<=ord(i)<=90) n_LETTER=len(LETTER) letter=''.join(i for i in s if 97<=ord(i)<=122) n_letter=len(letter) #print(n_LETTER,n_letter) if n_LETTER==0 and n_letter==0: score += 0 elif (n_LETTER>0 and n_letter==0) or (n_LETTER==0 and n_letter>0): score += 10 elif n_LETTER>0 and n_letter>0: score += 20 # 数字得分 num=''.join(i for i in s if 48<=ord(i)<=57) n_num=len(num) if n_num==0: score += 0 elif n_num==1: score += 10 elif n_num>1: score += 20 # 符号得分 asc=list(range(1,128)) #print(asc) symbol_ascii=[ii for ii in asc if ii not in asc[47:57] and ii not in asc[64:90] and ii not in asc[96:122]] symbol=''.join(i for i in s if ord(i) in symbol_ascii) n_symbol=len(symbol) if n_symbol==0: score += 0 elif n_symbol==1: score += 10 elif n_symbol>1: score += 25 # 奖励得分 if n_LETTER!=0 and n_letter==0 and n_num!=0 and n_symbol==0: score += 2 elif n_LETTER==0 and n_letter!=0 and n_num!=0 and n_symbol==0: score += 2 elif n_LETTER==0 and n_letter!=0 and n_num!=0 and n_symbol!=0: score += 3 elif n_LETTER!=0 and n_letter==0 and n_num!=0 and n_symbol!=0: score += 3 elif n_LETTER!=0 and n_letter!=0 and n_num!=0 and n_symbol!=0: score += 5 ''' 下方简化: ben = 0 if n_letter != 0 or n_LETTER != 0: if n_num != 0: ben = 2 if n_symbol != 0: ben = 3 if n_letter != 0 and n_LETTER !=0: ben = 5 score += ben ''' return score # 必须return得分,否则下一个函数无法生效 def SecurityLevel(score): if 0<=score<25: print('VERY_WEAK') elif 25<=score<50: print('WEAK') elif 50<=score<60: print('AVERAGE') elif 60<=score<70: print('STRONG') elif 70<=score<80: print('VERY_STRONG') elif 80<=score<90: print('SECURE') elif score>=90: print('VERY_SECURE') '''判断大写字母.isupper() 判断小写字母.islower() 判断数字 .isdigit() 判断符号 .isascii() 放在最后,不满足前述条件,剩下的ascii就是符号 ''' while 1: try: s=input() s_score=Score(s) SecurityLevel(s_score) except: break