CSDN Python学习班 大习题 (二)

练习题目:编写一个函数,接收一个字符串作为参数,判断该字符串作为密码的话其安全强度如何。如果字符串中只包含大写字母、小写字母、数字字符或标点符号中的一种则为弱密码,包含两种为中低安全密码,包含三种为中高安全密码,包含四种则为强密码。(额外要求:不可以使用正则表达式)


参考代码

 import string

def check(pwd):
    #密码必须至少包含6个字符
    if not isinstance(pwd, str) or len(pwd)<6:
        return 'not suitable for password'
    #密码强度等级与包含字符种类的对应关系
    d = {1:'weak', 2:'below middle', 3:'above middle', 4:'strong'}
    #分别用来标记pwd是否含有数字、小写字母、大写字母和指定的标点符号
    r = [False] * 4
    for ch in pwd:
        #是否包含数字
        if not r[0] and ch in string.digits:
            r[0] = True
        #是否包含小写字母
        elif not r[1] and ch in string.ascii_lowercase:
            r[1] = True
        #是否包含大写字母
        elif not r[2] and ch in string.ascii_uppercase:
            r[2] = True
        #是否包含指定的标点符号
        elif not r[3] and ch in ',.!;?<>':
            r[3] = True
    #统计包含的字符种类,返回密码强度
    return d.get(r.count(True), 'error')

#思路将酱紫. 标点符号的范围肯定不止这么大. 不知道有多大.
str = 'azAZ190,./'
def strongJudge(str):
charArray = list(str)
lv = {}
for c in charArray:
i = ord(c)
print(c,'=',i)
if i >=97 and i <= 122:
lv['t0'] = 1
if i >=65 and i <= 90:
lv['t1'] = 1
if i >=48 and i <= 57:
lv['t2'] = 1
if i>=44 and i <= 47:
lv['t4'] = 1
lens = len(lv)
if lens == 1:
print("密码强度弱")
if lens == 2:
print("密码强度中")
if lens == 3:
print("密码强度中高")
if lens == 4:
print("密码强度高")

strongJudge(str)

 #思路将酱紫. 标点符号的范围肯定不止这么大. 不知道有多大.
str = 'azAZ190,./'
def strongJudge(str):
    charArray = list(str)
    lv = {}
    for c in charArray:
        i = ord(c)
        print(c,'=',i)
        if i >=97 and i <= 122:
            lv['t0'] = 1
        if i >=65 and i <= 90:
            lv['t1'] = 1
        if i >=48 and i <= 57:
            lv['t2'] = 1
        if i>=44 and i <= 47:
            lv['t4'] = 1
    lens = len(lv)
    if lens == 1:
        print("密码强度弱")
    if lens == 2:
        print("密码强度中")
    if lens == 3:
        print("密码强度中高")
    if lens == 4:
        print("密码强度高")


strongJudge(str)

def pwd_check(pwd):
"""
判断该字符串作为密码的话其安全强度如何;如果字符串中只包含大写字母、小写字母、数字字符或标点符号中的一种则为弱密码,
包含两种为中低安全密码,包含三种为中高安全密码,包含四种则为强密码
:param pwd:
:return:-1 错误,1 弱密码,2 低安全密码,3 中高安全密码,4 强密码
"""
try:
result = "1234"
for one_chr in pwd:
if 48 <= ord(one_chr) <= 57:
result = result.replace("1", "0")
elif 65 <= ord(one_chr) <= 90:
result = result.replace("2", "0")
elif 97 <= ord(one_chr) <= 122:
result = result.replace("3", "0")
elif 33 <= ord(one_chr) <= 47 or 58 <= ord(one_chr) <= 63:
result = result.replace("4", "0")
else:
print("wrong charactor")
return -1
print("result str = %s", result)
return len(result.split("0")) - 1
except Exception as e:
print("pwd_check catch exception:%s", e)
return -1

import string

lower = string.ascii_lowercase
upper = string.ascii_uppercase
number = string.digits
special = string.punctuation

def strong_passwd(string):
strong = [0, 0, 0, 0]
for i in string[:]:
if i in lower:
strong[0] += 1
elif i in upper:
strong[1] += 1
elif i in number:
strong[2] += 1
elif i in special:
strong[3] += 1
else:
pass

count = 4-strong.count(0)
if count == 4:
    print("强密码")
elif count == 3:
    print("中强密码")
elif count == 2:
    print("中低密码")
elif count == 1:
    print("弱密码")
else:
    print("无效密码")

strong_passwd(" ")
strong_passwd("ASD")
strong_passwd("ASDasd")
strong_passwd("ASDasd123")
strong_passwd("ASDasd123,./")

 import string

lower = string.ascii_lowercase
upper = string.ascii_uppercase
number = string.digits
special = string.punctuation

def strong_passwd(string):
    strong = [0, 0, 0, 0]
    for i in string[:]:
        if i in lower:
            strong[0] += 1
        elif i in upper:
            strong[1] += 1
        elif i in number:
            strong[2] += 1
        elif i in special:
            strong[3] += 1
        else:
            pass

    count = 4-strong.count(0)
    if count == 4:
        print("强密码")
    elif count == 3:
        print("中强密码")
    elif count == 2:
        print("中低密码")
    elif count == 1:
        print("弱密码")
    else:
        print("无效密码")

strong_passwd(" ")
strong_passwd("ASD")
strong_passwd("ASDasd")
strong_passwd("ASDasd123")
strong_passwd("ASDasd123,./")
def key_strength(strs):
    symbols = r'''`!@#$%^&*()_+-=/*{}[]\|'";:/?,.<>'''
    chars = 'abcdefghijklmnopqrstuvwxyz'
    Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    nums = '0123456789'

    flag_con=0
    flag_con1=0
    flag_con2=0
    flag_con3=0
    for each in strs :
        if each in symbols:
            flag_con +=1
            break
    for each in strs :
        if each in chars:
            flag_con1 +=1
            break
    for each in strs :
        if each in Chars:
            flag_con1 +=1
            break
    for each in strs :
        if each in nums:
            flag_con2 +=1
            break
    sum=flag_con+flag_con1+flag_con2+flag_con3
    if sum==1 :
        print('弱密码')
    elif sum==2:
        print('中低安全密码')
    elif sum==3:
        print('中高安全密码')
    elif sum==4:
        print('强密码')

from string import ascii_lowercase, ascii_uppercase, digits, punctuation

def Strength(password):

    PsdList = [ascii_lowercase, ascii_uppercase, digits, punctuation]
    L = [0, 0, 0, 0]
    for p in password:
        if p in PsdList[0]:
            L[0] += 1
        elif p in PsdList[1]:
            L[1] += 1
        elif p in PsdList[2]:
            L[2] += 1
        elif p in PsdList[3]:
            L[3] += 1
        else:
            pass

    count = L.count(0)
    if count == 4:
        print("InvalidPassword")
    elif count == 3:
        print("low")
    elif count == 2:
        print("middlelow")
    elif count == 1:
        print("middlehigh")
    else:
        print("high")

if __name__ == '__main__':
    print(Strength(""))
        ```