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

时间:2018-08-24 12:39:22

标签: python regex python-3.x passwords

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

以下是检查密码的标准:

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

Answers to this question couldn't clear the problems

我尝试过,但是没有用

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')

1 个答案:

答案 0 :(得分:-1)

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

但是使用代码,您了解它可以做到:

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')