我是 Python 和编程新手。我正在尝试制作一个程序来验证输入的密码是否具有特定格式。我很确定我的代码是正确的,但显然......它不是。当密码格式正确时,它不会退出 while 循环。 我的错误在哪里?感谢大家的耐心等待!
low = ['abcdefghijklmnopqrstuvwxyz']
up = ['ABCDEFGHIJKLMNOPQRSTUVWXYZ']
cr = ['@$%&']
digito = ['0123456789']
counter1 = 0
tries = True
while tries:
length = False
May = False
Minus = False
Num = False
Char = False
counter1 += 1
password = input('Password: ')
if len(password) > 7 and len(password) < 16:
length = True
for caracter in password:
if caracter in low:
Minus = True
if caracter in up:
May = True
if caracter in cr:
Char = True
if caracter in digito:
Num = True
if length and May and Minus and Num and Char:
print('Password ok.')
passw = password
tries = False
if counter1 == 5:
print('You had 5 attemps.')
break
最佳答案
问题是当您检查时:
"A" in ['ABCDEFGHIJKLMNOPQRSTUVWXYZ']
它返回 False。这是因为列表 ['ABCDEFGHIJKLMNOPQRSTUVWXYZ']
不包含元素 "A"
。尝试用以下代码替换前 4 行,看看它是否有效:
low = 'abcdefghijklmnopqrstuvwxyz'
up = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
cr = '@$%&'
digito = '0123456789'
关于python - 我的错误在哪里?在Python中检查密码是否正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72604770/