Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is this while loop running endlessly?


def get_password():
    password = passwd() #returns None when no input is given else return password
    if password is not None:
        if len(password)<7:
            print("Minimum length of password is 6. Please try again.")
            password = None
        elif len(password)>16:
            print("Maximum length of password is 16. Please try again.")
            password = None
        while password is None:
            get_password()
    return password

I want to run this function again and again until users inputs a valid password. If user gives a valid password then while loop should not be entered but even then the loop is getting executed endlessly.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Don’t use recursivity to have a logic of "do that again", because when you need to stop it, recursivity is for specific problems

Use a main loop based on password is None, then call passwd and apply your rules

def get_password():
    password = None
    while password is None:
        password = passwd()  # returns None when no input is given else return password
        if password is not None:
            if len(password) < 7:
                print("Minimum length of password is 6. Please try again.")
                password = None
            elif len(password) > 16:
                print("Too Long", "Maximum length of password is 16. Please try again.")
                password = None
    return password
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading