4 回答

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊
沒有正則表達(dá)式你可以使用any
和str.isnumeric
if not any(map(str.isnumeric, password): print('No numbers')

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊
許多 python 在線密碼檢查器,例如:https ://www.geeksforgeeks.org/password-validation-in-python/
這是一個(gè)快速的控制臺(tái)程序,您可以像這樣調(diào)用:
$ python3 password_checker.py "Testf7788790##$"
Testing password: Testf7788790##$
Password is valid: True
$ python3 password_checker.py "insecurePassword"
Testing password: insecurePassword
Password should contain at least one number
Password is valid: False
的內(nèi)容password_checker.py:
#!/usr/bin/python
import sys
def password_check(passwd):
symbols = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=']
isValid = False
if len(passwd) < 10:
print('Password should be at least 10 characters')
elif not any(char.isdigit() for char in passwd):
print('Password should contain at least one number')
elif not any(char.isupper() for char in passwd):
print('Password should contain at least one uppercase character')
elif not any(char.islower() for char in passwd):
print('Password should contain at least one lowercase character')
elif not any(char in symbols for char in passwd):
print('Password should contain at least one special character from list: ', symbols)
else:
isValid = True
return isValid
arguments = sys.argv
if len(arguments) < 2:
print('No password could be parsed by argv')
valid_password = False
else:
password = arguments[1]
print('Testing password: ', password)
valid_password = password_check(password)
print('Password is valid: ', valid_password)

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
(其他答案已經(jīng)給了你很好的答案,所以這不是一個(gè)完整的答案,它只是對(duì)出了什么問題的解釋。)導(dǎo)致問題的代碼區(qū)域是
for x in password:
if x.isupper():
pass
else:
print('no upper case')
for y in password:
if y.islower():
pass
else:
print('no lower case')
您正在遍歷整個(gè)密碼,檢查每個(gè)字符是否為大寫,如果不是,則打印出“無大寫”。問題是如果單詞的單個(gè)字符不是大寫,"that_character_that_isn't_uppercase".isupper() 將返回 false,并打印錯(cuò)誤語句。例如,密碼 PaSSWORD 將返回一個(gè)“無大寫字母”,因?yàn)椤癮”.isupper() 為 False。密碼 passworD 將返回 7 個(gè)“無大寫字母”,因?yàn)樽址?p、a、s、s、w、o、r 都是小寫字母。x.islower() 測(cè)試也發(fā)生了同樣的事情,您正在查看每個(gè)字符是否都是小寫的。我會(huì)實(shí)施這樣的事情:
#password.islower() will return true if all the entire string is lowercase(and thus not uppercase)
if password.islower():
print("No upper case")
elif password.isupper():
print("No lower case")
#Again, password.isupper() sees if all letters are uppercase(which means that there is no lowercase letters).
希望這有幫助!

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊
我寧愿使用正則表達(dá)式。
In [122]: def validate(password):
...: return True if re.match("(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}", password) else False
...:
In [123]: validate("helloas")
Out[123]: False
In [124]: validate("helH12asfgvGh")
Out[124]: True
添加回答
舉報(bào)