无法中断Python中的while循环

时间:2018-12-13 23:32:07

标签: 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: ')

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

Valid password attempt

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

Invalid Password attempt

我感谢您给予的所有帮助。

1 个答案:

答案 0 :(得分:1)

似乎您在检查后要退出第一个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!
>>> 
相关问题