程序检查密码强度(和/或操作员是否无法使用/检查字符串中的符号?)

时间:2018-11-23 18:06:38

标签: python python-3.x passwords

我正在制作一个密码程序,该程序根据密码的长度以及有多少个大写/小写字母,数字和符号来检查密码的强度。

以下是我到目前为止所拥有的程序,但是,每当我输入应该以“中”返回的密码时,它都不会起作用,而是给我“弱”或“强”的密码。 / p>

此外,我不知道如何检查字符串中的符号。关于如何实现这一目标以及实现该计划的任何建议?我只是要求简单的解释和建议,因为我只是中学的初学者。

非常感谢您的帮助。

password = None

print()
print("\nYour password should be between 6 and 12 characters .")
print()

password=(input("Please enter your password : "))

numbers= sum(1 for c in password if c.isdigit())
uppercase= sum(1 for c in password if c.isupper())
lowercase= sum(1 for c in password if c.islower())

if len(password) <6:
  print("\n\tThe password is too short .")
  print("\tTry a longer password .")
  print("\n\tLowercase Letters :  ", lowercase)
  print("\tUppercase Letters :  ", uppercase)
  print("\tNumbers: ", numbers)

if len(password) >12:
  print("\n\tThe password is too long .")
  print("\tTry a shorter password .")
  print("\n\tLowercase Letters :  ", lowercase)
  print("\tUppercase Letters :  ", uppercase)
  print("\tNumbers: ", numbers)

if len(password)>=6 and len(password)<=12:

  if password.lower()== password or password.upper()==password or password.isdigit()==password:
    print("\n\tYour password is weak . ")
    print("\n\tLowercase Letters :  ", lowercase)
    print("\tUppercase Letters :  ", uppercase)
    print("\tNumbers : ", numbers)

  elif password.lower()== password and password.upper()==password or password.isalnum()==password:
    print("\n\tYour password is medium .")
    print("\n\tLowercase Letters :  ", lowercase)
    print("\tUppercase Letters :  ", uppercase)
    print("\tNumbers : ", numbers)

  else:
   password.lower()== password and password.upper()==password and password.isalnum()==password
   print ("\n\tYour password is strong .")
   print("\n\tLowercase Letters :  ", lowercase)
   print("\tUppercase Letters :  ", uppercase)
   print("\tNumbers : ", numbers)

1 个答案:

答案 0 :(得分:0)

欢迎使用堆栈溢出Aidyn。我在您的代码中发现了一些问题。


条件

password.lower()== password and password.upper()==password or password.isalnum()==password

永远不会发生。第一部分password.lower()== password检查密码是否全部为小写。下一部分password.upper()==password检查是否全部为大写。密码不能同时全部为小写和全部大写。


password.isalnum()返回True或False。因此,您无需检查password.isalnum()==password


password.isalnum()==True

不在password.lower()== password and password.upper()==password and password.isalnum()==password条件之内,因此结果为True或False无关紧要,对程序没有任何影响。


也许你想做的是

elif

您可以通过执行以下操作检查符号数

   # If the password contains only lowercase characters or only uppercase or only digits, it is weak
   if password.lower() == password or password.upper() == password or password.isdigit() == password:
       ...

   # Otherwise, if the password does not have any symbols, it is medium strength
   elif password.isalnum():
       ...

   # Otherwise, it's strong.
   else:
       ...