4 回答
TA贡献1995条经验 获得超2个赞
没有正则表达式你可以使用any
和str.isnumeric
if not any(map(str.isnumeric, password): print('No numbers')
TA贡献1863条经验 获得超2个赞
许多 python 在线密码检查器,例如:https ://www.geeksforgeeks.org/password-validation-in-python/
这是一个快速的控制台程序,您可以像这样调用:
$ 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
的内容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贡献1797条经验 获得超6个赞
(其他答案已经给了你很好的答案,所以这不是一个完整的答案,它只是对出了什么问题的解释。)导致问题的代码区域是
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')
您正在遍历整个密码,检查每个字符是否为大写,如果不是,则打印出“无大写”。问题是如果单词的单个字符不是大写,"that_character_that_isn't_uppercase".isupper() 将返回 false,并打印错误语句。例如,密码 PaSSWORD 将返回一个“无大写字母”,因为“a”.isupper() 为 False。密码 passworD 将返回 7 个“无大写字母”,因为字符 p、a、s、s、w、o、r 都是小写字母。x.islower() 测试也发生了同样的事情,您正在查看每个字符是否都是小写的。我会实施这样的事情:
#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贡献1875条经验 获得超3个赞
我宁愿使用正则表达式。
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
添加回答
举报