Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python密码验证中特殊字符的识别_Python_Validation_Input_Passwords - Fatal编程技术网

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

python密码验证中特殊字符的识别,python,validation,input,passwords,Python,Validation,Input,Passwords,我正在做一个密码验证任务,程序会一直向用户询问有效密码,直到给出一个为止。我在检查输入字符串的特殊字符时遇到问题。当前,即使密码没有特殊字符,程序也会接受密码。我还想实现一个特性,在3次尝试失败后终止循环,但不确定在哪个循环中实现计数。 这是我的密码: import re specialCharacters = ['$', '#', '@', '!', '*'] def passwordValidation(): while True: password = inp

我正在做一个密码验证任务,程序会一直向用户询问有效密码,直到给出一个为止。我在检查输入字符串的特殊字符时遇到问题。当前,即使密码没有特殊字符,程序也会接受密码。我还想实现一个特性,在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()
重新导入
特殊字符=['$'、'#'、'@'、'!'、'*']
def passwordValidation():
尽管如此:
密码=输入(“请输入密码:”)
如果len(密码)<6:
打印(“您的密码必须至少包含6个字符。”)
elif重新搜索(“[0-9]”,密码)为无:
打印(“您的密码必须至少有一个数字”)
elif重新搜索(“[A-Z]”,密码)为无:
打印(“您的密码必须至少有一个大写字母。”)
elif re.search('specialCharacters',password)为无:
打印(“您的密码必须至少有一个特殊字符($,#,@,!,*)”)
其他:
打印(“祝贺您!您的密码有效。”)
打破
密码验证()

对于如此简单的事情,没有必要使用正则表达式。那么:

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


您的代码检查密码是否包含单词“specialCharacters”。首先,删除它周围的引号。其次,将变量的值转换为正则表达式:
specialCharacters=r“[\$\\\@!\*]”
specialCharacters = set('$#@!*')
...
elif not specialCharacters.intersection(password):