Buttscratcher
Buttscratcher

Reputation: 13

Making a valid password checker. Can not get my program to get through the if conditions to print a valid password

I have been assigned the following exercise as homework:

  1. Some Web sites impose certain rules for passwords. Write a function that checks whether a string is a valid password. Suppose the password rules are as follows:
    • A password must have at least eight characters.
    • A password must consist of only letters and digits.
    • A password must contain at least two digits. Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise.

I know there's a more efficient and proper way of doing this but i'm just starting out so I don't necessarily need to be those right now. Just want to finish this question.

The counter/ accumulator works and I don't get any errors but I can not fulfill the if condition correctly so that this program prints "valid password"

password = str(input("Enter in a password to be checked: "))

def valid_password_checker(password):

    from string import ascii_lowercase as alphabet

    digits = '0123456789'  # creates a string of digits
    digit = 0  # acc for digits
    length = 0 # acc for length

    for char in password:  # calls on each character in string
        if char in alphabet:
            length += 1
        if char in digits:
            digit += 1
            if digit >= 2:
                flag = True
                if length >= 8 and digit is True:
                    print("valid password")
                else:
                    print("Password does not contain enough characters or digits.")
            else:
                print("Password does not contain enough digits.")

valid_password_checker(password)

Upvotes: 1

Views: 3437

Answers (2)

ObGamEr OGE
ObGamEr OGE

Reputation: 23

you can write something like this:

password = str(input("What is the password that you want to validate: "))

def get_digits(password):
    return [i for i in password if i.isdigit()]


numbers = ''.join(get_digits(password))

if (len(password) < 8) or (len(numbers) < 2):
    print(password, "is an invalid password")
else:
    print(password, "is a valid password")

nice and simple.

Upvotes: 0

steliosbl
steliosbl

Reputation: 8921

The problem with your existing code is that the variable digit is a number, and therefore doing digit is True as you have done in your if statement, always return False. If you remove digit is True, then your existing solution will work. Take a look however at my version:

def valid(password):
    digits = 0
    characters = 0

    for char in password:
        if char.isalpha():
            characters += 1
        elif char.isdigit():
            digits += 1
            characters += 1

    if characters >= 8:
        if digits >= 2:
            print("Password is valid")
        else:
            print("Password doesn't contain enough digits")
    else:
        print("Password doesn't contain enough characters")

I have made the following modifications from your original:

  • Used the built-in function str.isdigit() for checking if a character is a digit.
  • Used the built-in function str.isalpha() for checking if a character is a letter of the alphabet
  • Moved everything but the counting operations outside of the for loop, so that the function doesn't print multiple things

If you want, you can undo the first two changes, if you're worried about your teacher knowing you asked for help. However, I wouldn't turn in the solution that prints "Password doesn't contain enough digits" as many times as there are characters in the inputted password.

Upvotes: 0

Related Questions