如何根据用户输入检查密码的有效性



检查用户输入的密码的有效性。

以下是检查密码的标准:

  1. a-z 之间至少有 1 个字母。
  2. 至少 1 个介于 0-9 之间的数字
  3. A-Z 之间至少有 2 个字母
  4. $#@,中至少 2 个字符。等
  5. 交易密码的最小长度:6
  6. 交易密码最大长度:12

这个问题的答案无法解决问题

我试过这个,但它不起作用

N = [1,2,3,4,5,6,7,8,9,0]
A = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
S = ['!','@','#','$','%','~','`','^','&','*','(',')','_','+','=','-']
pasw = input('Password: ')
if any((word in pasw for word in N,A,S)):
print ('OK')
else:
print ('TRY LATER')

最好的方法是按照建议使用正则表达式,但如果您不知道正则表达式是什么,那将是一个全新的世界。我建议你读一读。

但是使用您理解的代码可以完成:

pasw='PAssword1!!'
S = ['!','@','#','$','%','~','`','^','&','*','(',')','_','+','=','-']
upper,lower,number,special = 0,0,0,0
for n in pasw:
if n.islower():
lower=1
if n.isnumeric():
number=1
if n.isupper():
upper+=1
if n in S:
special+=1
if len(pasw) >= 6 and len(pasw) <= 12 and lower > 0 and number > 0 and special > 1 and upper > 1:
print('OK')
else:
print('TRY LATER')

最新更新