简体   繁体   English

使用 python 循环运行密码验证时遇到问题

[英]Trouble running a password validation while loop with python

I'm very new to python and I'm trying to use a while loop that will validate a password based on several criteria but ends one all the criteria is met.我对 python 非常陌生,我正在尝试使用一个 while 循环,该循环将根据多个标准验证密码,但最终满足所有标准。 Can any one give me some tips on how to make this work?任何人都可以给我一些关于如何完成这项工作的提示吗? I've tried using an else statement but for some reason it wont allow it.我尝试过使用 else 语句,但由于某种原因它不允许这样做。

def chk_password(): def chk_password():

#Display info about password requirements
print('\n\nYou need a new password')
print('It must have at least six characters, but not more than 15')
print('It cannot contain any variation of \"umgc\"')
print('It cannot contain spaces')
print('It must contain the \"*\" character\n')

password = input('\nPlease enter your password:\n')

def chk_minlength():
    if len(password) >= 6:
        return True
    else:
        return False
def chk_maxlength():
    if len(password) <= 15:
        return True
    else:
        return False
def chk_spaces():
    if ' ' in password:
        return False
    else: 
        return True
def chk_specchar():
    if '*' in password:
        return True
    else:
        return False
def chk_umgc():
    lower_password = password.lower()
    if 'umgc' in lower_password:
        return False
    else:
        return True

chk_minlength()
chk_maxlength()
chk_spaces()
chk_specchar()
chk_umgc()

while chk_minlength == False or chk_maxlength == False or chk_spaces == False or chk_specchar == False or chk_umgc == False:
    print('Your password was invalid. Please make the following corrections:')          
    if chk_minlength == False:
        print('Your password must have at least six characters')
    if chk_maxlength == False:
        print('Your password cannot have more than 15 characters')
    if chk_spaces == False:         
        print('Your password cannot any spaces')
    if chk_specchar == False:
        print('Your password must contain the \"*\" character.')
    if chk_umgc == False:
        print('Your password cannot contain any variation of \"umgc\"')
    password = input('\nPlease enter your password:\n')
    if chk_minlength == True and chk_maxlength == True and chk_spaces == True and chk_specchar == True and chk_umgc == True: 
        print('Your password met all the requirements. Thank you.')
        break

chk_password() chk_password()

This looks mostly correct;这看起来大部分是正确的; however you are never saving the return values of your chk_* methods.但是,您永远不会保存chk_*方法的返回值。 In the condition of your while loop you are checking if chk_minlength == True or... , which is checking if the value of the method definition is True not if the return value is True .在您的 while 循环条件下,您正在检查chk_minlength == True or... ,这是检查方法定义的值是否为True ,而不是如果返回值为True To fix this you can simply us the method calls rather than reference to the methods:要解决此问题,您可以简单地使用方法调用而不是引用方法:

while not (chk_minlength() or chk_maxlength() or chk_spaces() or chk_specchar() or chk_umgc()):
    pass

Since you reuse the values inside the while loop, it makes more sense here to store the values beforehand:由于您在 while 循环中重用了这些值,因此在这里预先存储这些值更有意义:

valid_min_length = chk_minlength()
valid_max_length = chk_maxlength()
no_spaces = chk_spaces()
no_specchar = chk_specchar()
no_umgc = chk_umgc()

while not (valid_min_length or valid_max_length or no_spaces or no_specchar or nor_umgc):
   pass

Try this:尝试这个:

def check_password(password):
    if len(password) <=6 or len(password) >=15: # Checks if password is between 6 and 15 charecters long
        return False
    if (' ' in password) or ('\n' in password) or ('*' in password): # Checking if there are any spaces, asteriks, or newlines are in the password
        return False

    if 'UMGC' in password.upper(): # Checking if the string 'UMGC' is in the password
        return False

    return True
    '''
    Since a return function will automatically exit the function,
    the only way for this line to be reached is if all the other if statements are not satisfied,
    meaning that your password is safe
    '''

print(check_password("Hello World"))
print(check_password("Hello"))

What we are doing is essentially going through each of your functions in if-statements.我们所做的实际上是在 if 语句中遍历您的每个函数。 Then, if any of those if statements have been satisfied, it will return a False value.然后,如果满足这些if语句中的任何一个,它将返回一个False值。 Otherwise, it will have to return a True value.否则,它将必须返回一个True值。 I recommend you add something to check if there are any numbers or uppercase characters in the password for practice.我建议您添加一些内容以检查密码中是否有任何数字或大写字符以进行练习。

#Display info about password requirements
print('\n\nYou need a new password')
print('It must have at least six characters, but not more than 15')
print('It cannot contain any variation of \"umgc\"')
print('It cannot contain spaces')
print('It must contain the \"*\" character\n')

password = input('\nPlease enter your password:\n')

def chk_minlength():
    return len(password) < 6
def chk_maxlength():
    return len(password) > 15
def chk_spaces():
    return ' ' in password
def chk_specchar():
    return not '*' in password
def chk_umgc():
    return 'umgc' in password.lower()

while chk_minlength() or chk_maxlength() or chk_spaces() or chk_specchar() or chk_umgc():
    print('Your password was invalid. Please make the following corrections:')          
    if chk_minlength():
        print('Your password must have at least six characters')
    if chk_maxlength():
        print('Your password cannot have more than 15 characters')
    if chk_spaces():         
        print('Your password cannot any spaces')
    if chk_specchar():
        print('Your password must contain the \"*\" character.')
    if chk_umgc():
        print('Your password cannot contain any variation of \"umgc\"')
    password = input('\nPlease enter your password:\n')
print('Your password met all the requirements. Thank you.')

edit: fixed a syntax error:/编辑:修复了语法错误:/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM