下面是我的代码,当运行它并输入umgc时,它将接受密码。有人能帮上忙吗?非常感谢!
def strong_password(password):
Symbol=['#']
umgc=['umgc', 'umgC', 'umGc', 'umGC', 'uMgc', 'uMgC', 'uMGc', 'uMGC', 'Umgc', 'UmgC', 'UmGc', 'UmGC', 'UMgc', 'UMgC', 'UMGc', 'UMGC']
returned=True
if len(password) <= 6:
print('the length of password should be at least 6 characters long')
returned=False
if len(password) >= 12:
print('the length of password should be not be greater than 12')
returned=False
if not any(char in Symbol for char in password):
print('the password should have the # symbol')
returned=False
if password[0] == '#':
print('The # symbol should not be in the beginning of the password')
returned=False
if password[-1] == '#':
print('The password must not end with the # symbol')
returned=False
if any(char in umgc for char in password):
print('Password must not contain "UMGC" in any combination of upper/lowercase letters')
returned=False
if returned:
print('Strong Password!')
return returned
使用一个集合进行成员资格测试。
更改
if any(char in umgc for char in password):
至
umgc = set('umgc')
if umgc.issubset(password.lower()):
由于您询问单词列表中是否有单个字符,您的原始操作失败。你的原件是这样做的:
>>> umgc=['umgc', 'umgC', 'umGc', 'umGC', 'uMgc', 'uMgC', 'uMGc', 'uMGC', 'Umgc', 'UmgC', 'UmGc', 'UmGC', 'UMgc', 'UMgC', 'UMGc', 'UMGC']
>>> password = 'Umgc'
>>> for char in password:
... print(f'is {char} in {umgc}')
...
is U in ['umgc', 'umgC', 'umGc', 'umGC', 'uMgc', 'uMgC', 'uMGc', 'uMGC', 'Umgc', 'UmgC', 'UmGc', 'UmGC', 'UMgc', 'UMgC', 'UMGc', 'UMGC']
is m in ['umgc', 'umgC', 'umGc', 'umGC', 'uMgc', 'uMgC', 'uMGc', 'uMGC', 'Umgc', 'UmgC', 'UmGc', 'UmGC', 'UMgc', 'UMgC', 'UMGc', 'UMGC']
is g in ['umgc', 'umgC', 'umGc', 'umGC', 'uMgc', 'uMgC', 'uMGc', 'uMGC', 'Umgc', 'UmgC', 'UmGc', 'UmGC', 'UMgc', 'UMgC', 'UMGc', 'UMGC']
is c in ['umgc', 'umgC', 'umGc', 'umGC', 'uMgc', 'uMgC', 'uMGc', 'uMGC', 'Umgc', 'UmgC', 'UmGc', 'UmGC', 'UMgc', 'UMgC', 'UMGc', 'UMGC']
输入"umgc"
将接受密码,因为在这一行中:
if any(char in umgc for char in password):
您正在询问单词列表umgc
:中的密码(word(中是否有字符
然而,这将起作用:
if any(char in words for words in umgc for char in password):
这里我们要问的是,密码中是否有任何字符与单词列表中任何单词的任何字符相匹配。
更好的解决方案已经在答案中
希望我能帮助