0

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:

Valid password attempt

When an invalid password is entered, it outputs:

Invalid Password attempt

I appreciate all help given.

2
  • 1
    This would probably be easier if you put all those conditions into their own function and called that function to validate the password. You can even return early instead of using that flag and all those break statements. Commented Dec 13, 2018 at 23:33
  • It looks like you will break out of the while True on the first iteration no matter what the conditions are. Why not just remove it? Commented Dec 13, 2018 at 23:36

1 Answer 1

1

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!
>>> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.