不,我不是在诅咒标题。我需要创建一个密码处理程序来检查输入是否符合某个条件,其中一个条件是它必须包含一个字符$ @!%& * _。 这就是我现在所拥有的。
def pword():
global password
global lower
global upper
global integer
password = input("Please enter your password ")
length = len(password)
lower = sum([int(c.islower()) for c in password])
upper = sum([int(c.isupper()) for c in password])
integer = sum([int(c.isdigit()) for c in password])
def length():
global password
if len(password) < 8:
print("Your password is too short, please try again")
elif len(password) > 24:
print("Your password is too long, please try again")
def strength():
global lower
global upper
global integer
if (lower) < 2:
print("Please use a mixed case password with lower case letters")
elif (upper) < 2:
print("Please use a mixed case password with UPPER case letters")
elif (integer) < 2:
print("Please try adding numbers")
else:
print("Strength Assessed - Your password is ok")
答案 0 :(得分:0)
这种事情会起作用:
required='$@!%&*_'
def has_required(input):
for char in required:
if input.contains(char):
return True
return False
has_required('Foo')
答案 1 :(得分:0)
must_have = '$@!%&*_'
if not any(c in must_have for c in password):
print("Please try adding %s." % must_have)
如果any(c in must_have for c in password)
中的任何一个字符也在password
中, must_have
将返回True,换句话说,它将返回True,密码是好的。因为要测试错误的密码,我们将not
放在它前面以反转测试。因此,此处仅针对错误密码执行print
语句。
答案 2 :(得分:0)
您可以使用列表推导+内置any()轻松实现此目的。
has_symbol = any([symbol in password for symbol in list('$@!%&*_')])
或者更多分手:
required_symbols = list('$@!%&*_')
has_symbol = any([symbol in password for symbol in required_symbols])