我有以下代碼爲強度測試密碼:密碼測試在Python
import string
def golf(password):
if len(password) >= 10:
if any(char.isdigit() for char in password):
if any(string.punctuation for char in password):
if any(char.isalpha() for char in password):
if any(char.isupper() for char in password):
if any(char.islower() for char in password):
return True
return False
我知道這是可以做的更好!它需要測試以下...
密碼將被認爲是足夠強大,如果它 有至少10個字符 至少包含一個數字 包含至少一個大寫字母 包含至少一個小寫字母。密碼可能只包含ASCII拉丁字母或數字,但否標點符號。
編輯
確定爲別人我得到它下降到與正則表達式如下。
import re
def golf(password):
return re.match(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.{10,30}).+$', password)
顯然,它仍然是短...
你不能使用'和'? –
'如果有(字符串中的字符串的密碼):'看起來不對 –
你有沒有考慮過正則表達式匹配? –