简体   繁体   中英

Cannot break a while loop in Python

The problem is that I have certain requirements that need to be met as a verification for the password. So when I enter the requirements correctly, there is no problem and the program continues. However, when one of the requirements is not met it enters the while loop like it's supposed to, but then won't break out of it once the requirements are met. Can someone help me understand what is wrong?

By the way, I'm importing re module.

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

When a valid password is entered, it outputs:

有效的密码尝试

When an invalid password is entered, it outputs:

无效的密码尝试

I appreciate all help given.

It seems that while you're breaking out of the first while loop after checking, you are not doing the same again... Once the flag is set to -1, you stay in while flag == -1: because you never recheck the input again...

Make the pw checker its own function and while the return code of the function is not 0, keep asking for a password... I've tried the following and it works...

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

Output looks something like this...

>>> 
========================= 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!
>>> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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