更新时间:2023-11-26 09:35:58
您的测试无法进行,因为您正在将密码(类型为str
:字符串)与list
进行比较.由于对象是不可比较的,因此结果只是False
(即使它们是 可比较的,此处也不存在相等性,而是要检查的非空交集)
Your test cannot work because you're comparing your password (of type str
: string) against a list
. Since objects are non-comparable, the result is just False
(even if they were comparable there is no equality here, but a non-empty intersection to check)
您需要检查每个列表中密码中至少有1个成员
You need to check for each list that there's at least 1 member of the list in the password
使用any
定义一个辅助功能,该功能检查列表中的字母是否在密码中(lambda可能太多):
Define an aux function which checks if a letter of the list is in the password (a lambda would be too much maybe) using any
:
def ok(passwd,l):
return any(k in passwd for k in l)
然后使用all
在这种情况下测试所有四个列表:
Then test all your four lists against this condition using all
:
elif len(ww) >= 6 and len(ww)<= 12:
sww = set(ww)
if all(ok(sww,l) for l in [klein,groot,nummers,symbolen]):
print ("uw wachtwoord is Sterk")
请注意通过将密码(这是一个列表,对于in
运算符为O(n))转换为字符set
的字符(其中存在in
运算符,但速度更快)的略微优化.此外,集合将删除重复的字符,这对于本示例来说是完美的.
Note the slight optimization by converting the password (which is kind of a list so O(n) for in
operator) by a set
of characters (where the in
operator exists but is way faster). Besides, a set will remove duplicate characters, which is perfect for this example.
更紧凑的版本,没有aux功能,使用lambda
毕竟不是那么难理解:
More compact version without the aux function and using a lambda
which is not so difficult to understand after all:
elif len(ww) >= 6 and len(ww)<= 12:
sww = set(ww)
if all(lambda l: any(k in sww for k in l) for l in [klein,groot,nummers,symbolen]):
print ("uw wachtwoord is Sterk")