Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python循环时运行密码验证时出现问题_Python_Loops_Passwords - Fatal编程技术网

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

使用python循环时运行密码验证时出现问题,python,loops,passwords,Python,Loops,Passwords,我对python非常陌生,我正在尝试使用while循环,它将根据几个标准验证密码,但在满足所有标准后结束。有谁能给我一些如何使这项工作的建议吗?我尝试过使用else语句,但由于某些原因,它不允许使用它 def chk_密码(): #显示有关密码要求的信息 打印('\n\n您需要一个新密码') 打印('必须至少包含六个字符,但不能超过15') 打印('它不能包含任何\“umgc\”的变体) 打印('它不能包含空格') 打印('它必须包含\“*\”字符\n') 密码=输入('\n请输入密码:\n')

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

def chk_密码():

#显示有关密码要求的信息
打印('\n\n您需要一个新密码')
打印('必须至少包含六个字符,但不能超过15')
打印('它不能包含任何\“umgc\”的变体)
打印('它不能包含空格')
打印('它必须包含\“*\”字符\n')
密码=输入('\n请输入密码:\n')
def chk_minlength():
如果len(密码)>=6:
返回真值
其他:
返回错误
def chk_maxlength():
如果len(密码)请尝试以下操作:

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"))
def检查密码(密码):
如果len(password)=15:#检查密码长度是否在6到15个字符之间
返回错误
如果(“”在密码中)或('\n'在密码中)或('*'):#检查密码中是否有空格、星号或换行符
返回错误
如果password.upper()中有'UMGC',:#检查密码中是否有字符串'UMGC'
返回错误
返回真值
'''
由于返回函数将自动退出该函数,
达到这一行的唯一方法是如果所有其他if语句都不满足,
这意味着你的密码是安全的
'''
打印(检查密码(“Hello World”))
打印(检查密码(“你好”))
我们所做的基本上是在if语句中遍历每个函数。然后,如果这些
if
语句中的任何一条得到满足,它将返回一个
False
值。否则,它将必须返回
True
值。我建议您添加一些内容来检查密码中是否有任何数字或大写字符,以便练习。

尝试以下操作:

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"))
#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检查密码(密码):
如果len(password)=15:#检查密码长度是否在6到15个字符之间
返回错误
如果(“”在密码中)或('\n'在密码中)或('*'):#检查密码中是否有空格、星号或换行符
返回错误
如果password.upper()中有'UMGC',:#检查密码中是否有字符串'UMGC'
返回错误
返回真值
'''
由于返回函数将自动退出该函数,
达到这一行的唯一方法是如果所有其他if语句都不满足,
这意味着你的密码是安全的
'''
打印(检查密码(“Hello World”))
打印(检查密码(“你好”))
我们所做的基本上是在if语句中遍历每个函数。然后,如果这些
if
语句中的任何一条得到满足,它将返回一个
False
值。否则,它将必须返回
True
值。我建议您添加一些内容,以检查密码中是否有任何数字或大写字符,以便练习。

#显示有关密码要求的信息
#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.')
打印('\n\n您需要一个新密码') 打印('必须至少包含六个字符,但不能超过15') 打印('它不能包含任何\“umgc\”的变体) 打印('它不能包含空格') 打印('它必须包含\“*\”字符\n') 密码=输入('\n请输入密码:\n') def chk_minlength(): 返回len(密码)<6 def chk_maxlength(): 返回len(密码)>15 def chk_空格(): 在密码中返回“” def chk_specchar(): 在密码中不返回“*” def chk_umgc(): 在password.lower()中返回“umgc” 而chk_minlength()或chk_maxlength()或chk_spaces()或chk_specchar()或chk_umgc(): print('您的密码无效。请进行以下更正:') 如果chk_minlength(): 打印('您的密码必须至少包含六个字符') 如果chk_maxlength(): 打印('您的密码不能超过15个字符') 如果chk_空格() 打印('您的密码不能包含任何空格') 如果chk_specchar(): 打印('您的密码必须包含\“*\”字符。') 如果chk_umgc(): 打印('您的密码不能包含\“umgc\”的任何变体') 密码=输入('\n请输入密码:\n') 打印('您的密码符合所有要求。谢谢')
编辑:修复了语法错误:///p>
#显示有关密码要求的信息
打印('\n\n您需要一个新密码')
打印('必须至少包含六个字符,但不能超过15')
打印('它不能包含任何\“umgc\”的变体)
打印('它不能包含空格')
打印('它必须包含\“*\”字符\n')
密码=输入('\n请输入密码:\n')
def chk_minlength():
返回len(密码)<6
def chk_maxlength():
返回len(密码)>15
def chk_空格():
在密码中返回“”
def chk_specchar():
在密码中不返回“*”
def chk_umgc():
在password.lower()中返回“umgc”
而chk_minlength()或chk_maxlength()或chk_spaces()或chk_specchar()或chk_umgc():
print('您的密码无效。请进行以下更正:')
如果chk_minlength():
打印('您的密码必须至少包含六个字符')
如果chk_maxlength():
打印('您的密码不能超过15个字符')
如果chk_空格()
打印('您的密码不能包含任何空格')
如果chk_specchar():
打印('您的密码必须包含\“*\”字符。')
如果chk_umgc():
打印('您的密码不能包含\“umgc\”的任何变体')
密码=输入('\n请输入密码:\n')
打印('您的密码符合所有要求。谢谢')

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

这看起来基本正确;但是,您永远不会保存
chk.*
方法的返回值。在你的while循环中