檢查並判斷密碼字符串的安全強度
import string def check(pwd): #密碼必須至少包含六個字符 if not isinstance(pwd,str) or len(pwd)<6: return 'noot suitable for password' #密碼強度等級與包含字符種類的對應關系 d = {1:'weak',2:'below middle',3:'above middle',4:'strong'} #分別用來標記pwd是否含有數字、小寫字母、大寫字母、指定的標點符號 r = [False]*4 pwd_range = string.ascii_uppercase+string.ascii_lowercase+string.digits+',.!;><?' for ch in pwd: if ch not in pwd_range: return 'error' elif 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') def program(): while True: pwd = input("請輸入您的密碼:") print(check(pwd)) program()