为Python 3创建密码标准模块

时间:2018-03-08 03:30:05

标签: python module passwords

所以这就是问题所在。我有一系列函数,用户可以检查来自用户的inputString以满足所有设置的密码标准:

  • 密码长度必须至少为5个字符
  • 密码必须至少包含一个大写字母
  • 密码必须至少包含两个数字
  • 密码不得包含字符“E”或“e”
  • 密码必须包含至少一个非字母数字字符。
  • 密码可能不是常用的密码:'password','12345', 'qwerty','letmein','trustno1','000000','passw0rd,'密码'

我的上一个函数试图收集定义到单个可用模块函数中的所有函数。运行该程序没有错误,但有一个错误始终打印,“无效!密码必须包含特殊字符。”为什么会这样?还有什么其他的错误或修复你们建议使这个代码更有效或可读?

def isFiveCharacters(inputString):
    while len(inputString) > 5:
        return True #print('Contains at least 5 characters, ')
    else:
        print('Invalid! Password must cantain more than 5 characters')
    return False
def hasUpperCase(inputString):
    x = any(char.isupper() for char in inputString)
    if x == True:
        return True #print ('an uppercase letter, ')
    if x == False:
        print('Invalid! Password must contain an upper case letter')
    return False
def hasNumbers(inputString):
    count = 0
    for char in inputString:
        if char == char.isdigit():
            count += 1
            if count >= 2:
                #print ('two numbers, ')
                return True
            elif count < 2:
                print ('Invalid! Password must contain two numbers')
                return False
def hasLetterE(inputString):
    for char in inputString:
        if 'E' and 'e' in inputString:
            print('Invalid! Password cannot contain the letter "E"')
            return False
        else:
            #print('does not contain the letter E, ')
            return True
    #if 'e' in inputString:
#        print('Password cannot contain the letter "e"')
#    return None
def nonAlphaNumChar(inputString):
    special_char = ['!','@','$','%','#','^','&','*']
    if inputString == special_char * 2:
            #print('a special character, ')
        return True
    else:
        print('Invalid! Password must contain a special character')
    return None
def usedPasswords(inputString):
    used_passwords = ('password','12345','qwerty','letmein','trustno1','000000','passw0rd','Password')
    if used_passwords == inputString:
        print('Invalid! Password must be original.')
        return False
def passwordCriteria(inputString):
    isFiveCharacters(inputString)
    hasUpperCase(inputString)
    hasNumbers(inputString)
    hasLetterE(inputString)
    nonAlphaNumChar(inputString)
    usedPasswords(inputString)
    while inputString == True:
        print('Valid Password')
        return True
    if inputString == False:
        print('Error, invalid password')
        return False
    return None

1 个答案:

答案 0 :(得分:0)

我只想指出一个明显的错误:

你应该收集像这样的函数返回的值

valid = isFiveCharacters(inputString)
# then just use the boolean values with an `and`
valid = valid and hasUpperCase(inputString)
valid = valid and hasNumbers(inputString)
# and so on
# then use
if valid:
    print("Valid Password")
else:
    print("Invalid Password")

我建议详细阅读return语句和while循环的函数,并清楚地了解它们的工作原理。