【发布时间】: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