How to check strength password with function and loop while checking off 3 criteria boxes in Python?

31 views Asked by At

I have an assignment to create a password check that says if the password is strong (3 criteria checked off) moderate (2 criteria checked off) or weak (1 or less). The criteria are: 1: containing 2 of more digits 2: length is 6 or more characters 3: containing 1 or more special characters: ~ !@ #$%^&*.

I have tried a lot and looked up but wanted to import nothing, because thats not allowed at my exam. I want to use a for loop, if statements and a definition function. And want to keep it simple, im just a beginner.

password= str(input("What is the password? "))
specchar= ["!","~","@", "#", "$", "%", "^","&", "*"]
a=0
for item in specchar:
    a= a+1
def CountDigitsFor(password):
    res = []
    for i in password:
        if i.isdigit():
            res.append(i)
    return len(res)
def result(len(res), a)
    return len(res)+a
b=0
if len(password)>=6:
    b = b+1
if b == 1 and result == 3:
    print("Password is strong")
if b+result == 3 or b+result ==2 :
    print("Password is moderately strong")
if b+result <= 2:
    print("Password is weak")

Nothing is the outcome except the user input. Could someone please help?

2

There are 2 answers

0
Barmar On

First, use more descriptive variable names than a and b.

Add a counter variable that counts the number of criteria that are satisfied.

You can use the built-in sum() function to get the number of characters that meet a condition.

password= str(input("What is the password? "))
specchar= {"!","~","@", "#", "$", "%", "^","&", "*"}
count_specchar = sum(c in specchar for c in password)
count_digits = sum(c.isdigit() for c in password)

criteria_count = 0
if count_digits >= 2:
    criteria_count += 1
if len(password) >= 6:
    criteria_count += 1
if count_specchar >= 1:
    criteria_count += 1

if criteria_count == 3:
    print("password is strong")
elif criteria_count == 2:
    print("password is moderately strong")
else:
    print("password is weak")

0
sahasrara62 On

writing each condition check with a separate function, and then calcuate the condition it matched and priniting result accordingly.

note : this can be optimize a lot, but just chossing a normal easy to go approach

password= str(input("What is the password? "))

def check_length(string):
    if len(string) >= 6:
        return True
    return False

def check_digits(string):
    digit_count = 0
    for char in string:
        if char.isdigit():
            digit_count += 1
    if digit_count >= 2:
        return True
    return False

def check_special_char(string):
    special_chars = ["!","~","@", "#", "$", "%", "^","&", "*"]
    special_chars_count = 0

    for char in string:
        if char in special_chars:
            special_chars_count += 1
    
    if special_chars_count >= 1:
        return True
    
    return False

password_strength = 0

if check_length(password):
    password_strength += 1

if check_digits(password):
    password_strength += 1

if check_special_char(password):
    password_strength += 1

if password_strength <= 1:
    print("Password is weak")
elif password_strength == 2:
    print("Password is moderately string")
elif password_strength == 3:
    print("Password is strong")