應用python語法, 快速解
檢查密碼特性:
len(p) >= 8
來確認密碼的長度是否至少為 8 個字符,結果存儲在 le
中。any(c.isupper() for c in p)
來確認是否至少有一個字符是大寫字母,結果存儲在 u
中。any(c.islower() for c in p)
來確認是否至少有一個字符是小寫字母,結果存儲在 lo
中。any(c.isdigit() or not c.isalnum() for c in p)
來確認密碼中是否包含至少一個數字或符號,結果存儲在 n_s
中。這裡的 not c.isalnum()
用於檢查符號。計算符合的特性數量: 使用 sum([le, u, lo, n_s])
獲得符合的特性數量,並將其存儲在變量 c
中。
評分類別:
c
等於 4,則打印 "This password is STRONG"。c
等於 3,則打印 "This password is GOOD"。c
等於 2,則打印 "This password is ACCEPTABLE"。c
等於 1 或 0,則打印 "This password is WEAK"。
while True:
try:
p=input() # p: password
le=len(p)>=8 # le: length
u=any(c.isupper() for c in p) # u: uppercase letter
lo=any(c.islower() for c in p) # lo: lowercase letters
n_s=any(c.isdigit() or not c.isalnum() for c in p) # n_s: numbers and symbols
c=sum([le, u, lo, n_s])
if c==4:
print("This password is STRONG")
elif c==3:
print("This password is GOOD")
elif c==2:
print("This password is ACCEPTABLE")
else:
print("This password is WEAK")
except:
break