繁体   English   中英

无法中断Python中的while循环

[英]Cannot break a while loop in Python

问题是我需要满足某些要求才能验证密码。 因此,当我正确输入需求时,就没有问题,程序继续进行。 但是,当其中一项要求未得到满足时,它会按预期方式进入while循环,但是一旦满足要求就不会中断。 有人可以帮助我了解问题在哪里吗?

顺便说一句,我正在导入re模块。

def input_password(self):
    print('Password must be 8-12 characters, must contain at least one uppercase and lowercase letter,'
          'and one number.')
    self.__input_password = input('Password: ')

    flag = 0
    while True:
        if len(self.__input_password) < 8:
            flag = -1
            break
        elif len(self.__input_password) > 12:
           flag = -1
           break
        elif not re.search("[a-z]", self.__input_password):
            flag = -1
            break
        elif not re.search("[A-Z]", self.__input_password):
            flag = -1
            break
        elif re.search("\s", self.__input_password):
            flag = -1
            break
        else:
            flag = 0
            print('Valid Password')
            break
    while flag == -1:
        print('Invalid Password. Please reenter.')
        print('Password must be 8-12 characters, must contain at least one uppercase and lowercase letter,'
              ' and one number.')
        self.__input_password = input('Password: ')

输入有效密码后,将输出:

有效的密码尝试

输入无效的密码后,将输出:

无效的密码尝试

我感谢所有的帮助。

似乎在检查后要退出第一个while循环时,您不会再做同样的事情了……一旦将标志设置为-1,您就停留在while flag == -1:因为您从未重新检查过再次输入...

使pw checker成为其自己的函数,并且该函数的返回代码不为0时,请继续要求输入密码...我尝试了以下操作,它确实有效...

import re

def pw_checker(pw):
    if len(input_password) < 8:
        return -1
    elif len(input_password) > 12:
        return -1
    elif not re.search("[a-z]", input_password):
        return -1
    elif not re.search("[A-Z]", input_password):
        return -1
    elif re.search("\s", input_password):
        return -1
    else:
        return 0


print('Password must be 8-12 characters, must contain at least one uppercase and lowercase letter, and one number.')
input_password = input('Password: ')

while pw_checker(input_password) is not 0:
    print('Invalid Password. Please reenter.')
    print('Password must be 8-12 characters, must contain at least one uppercase and lowercase letter,'
            ' and one number.')
    input_password = input('Password: ')

输出看起来像这样...

>>> 
========================= RESTART: D:\Python\test.py =========================
Password must be 8-12 characters, must contain at least one uppercase and lowercase letter, and one number.
Password: testing
Invalid Password. Please reenter.
Password must be 8-12 characters, must contain at least one uppercase and lowercase letter, and one number.
Password: Testing
Invalid Password. Please reenter.
Password must be 8-12 characters, must contain at least one uppercase and lowercase letter, and one number.
Password: testing123
Invalid Password. Please reenter.
Password must be 8-12 characters, must contain at least one uppercase and lowercase letter, and one number.
Password: Testing123
>>> 
========================= RESTART: D:\Python\test.py =========================
Password must be 8-12 characters, must contain at least one uppercase and lowercase letter, and one number.
Password: Testing123!
>>> 

暂无
暂无

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

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