編寫程式,檢查並判斷密碼字串的安全強度。
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')
print(check('目標密碼'))