【问题标题】:Identifying special characters in password verification in python在python中识别密码验证中的特殊字符
【发布时间】:2017-12-27 22:19:23
【问题描述】:

我正在处理一项密码验证任务,其中程序不断要求用户输入有效密码,直到给出密码。我在检查输入字符串是否有特殊字符时遇到问题。目前,即使密码不是特殊字符,该程序也接受密码。此外,我想实现一个功能,在 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()

【问题讨论】:

  • 您的代码检查密码是否包含单词“specialCharacters”。首先,删除它周围的引号。二、将变量的值转换为正则表达式:specialCharacters = r"[\$#@!\*]"

标签: python validation input passwords


【解决方案1】:

对于这么简单的事情没有必要使用正则表达式。怎么样:

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

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

【讨论】:

    猜你喜欢
    • 2020-10-05
    • 2021-10-12
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 2020-10-16
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多