我必须编写一个验证密码并返回true或false的函数。仅当它是8个字符,包含数字,大写字母和符号时才是真实的。
这就是我的功能文件。
def validatepassword(pswd):
for char in pswd:
if char in '01234567890':
containsnumber = True
我不知道如何合并其他变量,感谢任何帮助。
由于
答案 0 :(得分:0)
def validatepassword(pswd):
## Declare the flags and set them to False
containsNumber = False
containsUpper = False
containsSymbol = False
for char in pswd: ## Loop through the characters of the password
if char in '0123456789': ## Check if the character's a number
containsNumber = True
elif char in "ABCEDFGHIJKLMNOPQRSTUVWXYZ": ## Otherwise check if the character's an uppercase letter
containsUpper = True
elif char not in "0123456789ABCEDFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz": ## Otherwise check if the letter isn't an alphanumeric character (i.e. a symbol)
containsSymbol = True
if containsNumber and containsUpper and containsSymbol and len(pswd) == 8: ## Were all of the checks passed?
return True
return False ## No need for an 'else' as the program will only reach this stage if it hasn't returned anything yet
答案 1 :(得分:0)
使用正则表达式并在python中检查密码匹配,如下所示。
检查python文档以获取更多帮助https://docs.python.org/2/howto/regex.html
导入重新
def validatePassword(pswd):
P=re.compile('[\d\D]')
if(P.match(pswd)):
print('TRUE')
else:
print('FALSE')
validatePassword( '超级')