简体   繁体   English

在python中识别密码验证中的特殊字符

[英]Identifying special characters in password verification in python

I am working on an assignment for password validation where the program keeps asking the user for a valid password until one is given.我正在处理一项密码验证任务,其中程序不断要求用户输入有效密码,直到给出密码为止。 I am having trouble checking the input string for special characters.我在检查输入字符串中的特殊字符时遇到问题。 Currently the program accepts the password even if their is no special characters.目前该程序接受密码,即使它们没有特殊字符。 Also I would like to implement a feature that terminates the loop after 3 unsuccessful attempts but am not sure which loop to implement the count in. Here is my code:此外,我想实现一个功能,在 3 次尝试失败后终止循环,但我不确定在哪个循环中实现计数。这是我的代码:

import re

specialCharacters = ['$', '#', '@', '!', '*']

def passwordValidation():
    while True:
         password = input("Please enter a password: ")
        if len(password) < 6:
            print("Your password must be at least 6 characters.")
        elif re.search('[0-9]',password) is None:
            print("Your password must have at least 1 number")
        elif re.search('[A-Z]',password) is None:
            print("Your password must have at least 1 uppercase letter.")
        elif re.search('specialCharacters',password) is None:
            print("Your password must have at least 1 special character ($, #, @, !, *)")
        else:
            print("Congratulations! Your password is valid.")
            break
passwordValidation()

There is no need to use regular expression for something so simple.对于这么简单的事情,没有必要使用正则表达式。 How about:怎么样:

elif not any(c in specialCharacters for c in password):

or

specialCharacters = set('$#@!*')
...
elif not specialCharacters.intersection(password):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM