使用python循环时运行密码验证时出现问题

2024-09-25 10:27:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python非常陌生,我正在尝试使用while循环,它将根据几个标准验证密码,但在满足所有标准后结束。有谁能给我一些如何使这项工作的建议吗?我尝试过使用else语句,但由于某些原因,它不允许使用它

def chk_密码():

#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_密码()


Tags: falsetrueyourreturnifdefpasswordelse
3条回答

这看起来基本正确;但是,您永远不会保存chk_*方法的返回值。在while循环的条件下,检查ifchk_minlength == True or ...,这是检查方法定义的值是否为True,而不是检查返回值是否为True。要解决此问题,您可以简单地使用方法调用,而不是引用方法:

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

由于您在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
#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.')

编辑:修复了语法错误:/

试试这个:

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

我们所做的基本上是在if语句中遍历每个函数。然后,如果这些if语句中的任何一个已经满足,它将返回一个False值。否则,它将必须返回一个True值。我建议您添加一些内容来检查密码中是否有任何数字或大写字符,以便练习

相关问题 更多 >