检查密码是弱,中,强

时间:2017-06-18 13:53:42

标签: python

这是我的代码:

print ("Welcome to password generator!")
print ("We will check the level of your password for you!")
print ("There are 3 levels. Weak, Medium and Strong")
password = raw_input("Enter your password here: ")

weak = 'weak, add some extra characters, upper letters or numbers!'
medium = 'medium, you can add numbers to make it stronger!'
strong = 'strong, you are safe now!'

#Just making sure that password is ok
if len(password) > 12: 
    print ("Password must be longer than 6 and below 12!") 
elif len(password) < 6:
    print ("Password must be longer than 6 and below 12!") 


if password == password.lower or password == password.upper: #Check if password contain lower or upper letters
    print ('Your password is',weak)
if password.lower == password and password.upper == password: #Check if password contain lower and upper letters
    print ('Your password is',medium)
if password.lower == password and password.upper == password and any(str.isdigit(c) for c in password): #Check if password contain any number
    print ('Your password is',strong)

print ("\n")
if password == strong or password == medium or password == weak:
    print ("We are glad we help you, extra tip: Change password every 3-6 months!")

问题是当我输入密码时,例如“newpass”或“newpass1”我没有得到任何输出。我希望得到“你的密码很弱”或中等,...... 但没什么。

5 个答案:

答案 0 :(得分:2)

lowerupper是方法,即您需要使用()才能实际调用它们。

例如,if password == password.lower():

password = 'PASS'
print(password.lower)
#  <built-in method lower of str object at 0x02386020>
print(password.lower())
#  pass

答案 1 :(得分:0)

使用lowerupper作为方法:

if password == password.lower() or password == password.upper(): #Check if password contain lower or upper letters
    print ('Your password is',weak)
if password.lower() == password and password.upper() == password: #Check if password contain lower and upper letters
    print ('Your password is',medium)
if password.lower() == password and password.upper() == password and any(str.isdigit(c) for c in password): #Check if password contain any number
    print ('Your password is',strong)

答案 2 :(得分:0)

像这样检查大小写

if password.islower() or password.isupper():
    print ('Your password is',weak)
if password.islower() == False and password.isupper() == False:
    if any(str.isdigit(c) for c in password):
        print ('Your password is',strong)
    else:
        print ('Your password is',medium)

答案 3 :(得分:0)

让我们分析一下这种情况,在中等强度中使用

if password.lower() == password and password.upper() == password:

检查密码是全是大写字母还是全部小写字母。字母不能同时是大写和小写,因此代码永远不会执行。相反,你应该使用:

if password.lower() != password and password.upper() != password:

因为这可以确保密码不仅包含小写字母大写

答案 4 :(得分:0)

你应该在括号中使用password.lower()password.upper(),否则他们不会在调用时返回一个字符串,而是另一个对象和比较

password == password.lower

和其他类似的内容将始终返回False