我对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_密码()
这看起来基本正确;但是,您永远不会保存
chk_*
方法的返回值。在while循环的条件下,检查ifchk_minlength == True or ...
,这是检查方法定义的值是否为True
,而不是检查返回值是否为True
。要解决此问题,您可以简单地使用方法调用,而不是引用方法:由于您在while循环中重用了这些值,因此在这里提前存储这些值更有意义:
编辑:修复了语法错误:/
试试这个:
我们所做的基本上是在if语句中遍历每个函数。然后,如果这些
if
语句中的任何一个已经满足,它将返回一个False
值。否则,它将必须返回一个True
值。我建议您添加一些内容来检查密码中是否有任何数字或大写字符,以便练习相关问题 更多 >
编程相关推荐