Python Program to Check Password Validation

In this tutorial, we are going to discuss how to use Python while loop to check that the password is valid or not.

So, we will be asking the user to enter a password and validate it using a while loop.

The password should meet certain criteria, such as being at least 8 characters long and contain at least one uppercase letter, one lowercase letter, and one number.

Simple code:

print("Please enter a password to validate.")
password = input()

while True:
    if len(password) < 8:
        print("Password must be at least 8 characters long.")
    elif not any(char.isupper() for char in password):
        print("Password must contain at least one uppercase letter.")
    elif not any(char.islower() for char in password):
        print("Password must contain at least one lowercase letter.")
    elif not any(char.isdigit() for char in password):
        print("Password must contain at least one number.")
    else:
        print("Password is valid.")
        break
    password = input()

Output:

Please enter a password to validate: Ali87763.
Password is valid.

Code With Comments:

# Ask the user to enter a password to validate.
print("Please enter a password to validate.")

# Store the user's input in the password variable.
password = input()

# While loop to check password validation criteria
while True:
    # Check if the password is at least 8 characters long.
    if len(password) < 8:
        print("Password must be at least 8 characters long.")
    # Check if the password contains at least one uppercase letter.
    elif not any(char.isupper() for char in password):
        print("Password must contain at least one uppercase letter.")
    # Check if the password contains at least one lowercase letter.
    elif not any(char.islower() for char in password):
        print("Password must contain at least one lowercase letter.")
    # Check if the password contains at least one number.
    elif not any(char.isdigit() for char in password):
        print("Password must contain at least one number.")
    # If all criteria are met, print a success message and break out of the loop.
    else:
        print("Password is valid.")
        break

    # If the password is not valid, prompt the user to enter another password.
    password = input()

Explanation:

This program asks the user to enter a password and then checks if it meets certain criteria.

The password must be at least 8 characters long, and contain at least one uppercase letter, one lowercase letter, and one number.

The program uses a while loop to continue prompting the user for a valid password until all the criteria are met.

The program provides feedback to the user indicating which validation criteria they are missing until a valid password is entered.

Once a valid password is entered, the program prints a success message and exits the loop.


Discover more from Python Mania

Subscribe to get the latest posts sent to your email.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Related Articles:

Recent Articles:

0
Would love your thoughts, please comment.x
()
x

Discover more from Python Mania

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

Continue reading