Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

arrays - How to escape Iterated loop Python

Hey there,

I recently started learning python and Since I knew a bit of JavaScript, I got a little head start to python that what most beginner learners have when learning python. Since I started learning python, I have been taking challenges and working on my own little project in the mean time. The current challenge I have right now is to make a password validator.

My problem:

I have spent well over 2 hours trying to figure out how to count the number of special chars and numbers in a string (password). I still haven't perfected the counting but my main problem right now Is getting the function to print out the result

Right, so I have the counter here:

def password_validator():
    password = input("Please input your password >>> ")

    for element in password:

        num_list = ['1','2','3','4','5','6','7','8','9','0',]

        for x in num_list:
            num_count = 0
            if element.count(x):
                num_count = 1
                if element.count(x):
                    num_count = 2
                    num_pass_strength = 1
                    if num_count == 2:
                        num_pass_strength = 1
                else:
                    num_pass_strength = 0
            elif num_count == 0:
                num_pass_strength = 0

        spec_chars = ['!', '@', '#', '$', '%', '&', '*',]
        
        for c in spec_chars:
            spec_count = 0
            if element.count(c):
                spec_count = 1
                if element.count(c):
                    spec_count = 2
                    if spec_count == 2:
                        spec_pass_strength = 1
                else:
                    spec_pass_strength = 0
            elif spec_count == 0:
                spec_pass_strength = 0

This basically counts the number of digits and special chars in the password.

This is the how I calculate the password strength:

        add_strength = 0

        if num_pass_strength == 1:
            add_strength += 1
        if spec_pass_strength == 1:
            add_strength +=1

        pass_strength = add_strength

This adds one strength to the add_strength variable, and the pass_strength gets it's data from the add_strength variable aswell.

And this is what I think I am having trouble with:

            if pass_strength == 1:
                is_weak_pass = True
            elif pass_strength == 2:
                is_medium_pass = True
            elif pass_strength == 3:
                is_strong_pass = True

        if is_weak_pass == True:
            print("Weak")
        elif is_medium_pass == True:
            print("Medium")
        elif is_strong_pass == True:
            print("Strong")

This is supposed to check the value of the strength and define a bool - variable for the the desired output.

My main problem right now is though, when I run the script and enter the a password,
it leaves me with an out put like this

Output:
Please input your password >>> &^%&^%
Weak
Weak
Weak
Weak
Weak
Weak

Could somebody please tell me what I have done wrong. It is really bugging me and I would even love to figure out something like this for my other project. Thanks

Full Code:


def password_validator():
    password = input("Please input your password >>> ")

    for element in password:

        num_list = ['1','2','3','4','5','6','7','8','9','0',]

        for x in num_list:
            num_count = 0
            if element.count(x):
                num_count = 1
                if element.count(x):
                    num_count = 2
                    num_pass_strength = 1
                    if num_count == 2:
                        num_pass_strength = 1
                else:
                    num_pass_strength = 0
            elif num_count == 0:
                num_pass_strength = 0

        spec_chars = ['!', '@', '#', '$', '%', '&', '*',]
        
        for c in spec_chars:
            spec_count = 0
            if element.count(c):
                spec_count = 1
                if element.count(c):
                    spec_count = 2
                    if spec_count == 2:
                        spec_pass_strength = 1
                else:
                    spec_pass_strength = 0
            elif spec_count == 0:
                spec_pass_strength = 0

            add_strength = 0

            if num_pass_strength == 1:
                add_strength += 1
            if spec_pass_strength == 1:
                add_strength +=1

            pass_strength = add_strength
  
            if pass_strength == 1:
                is_weak_pass = True
            elif pass_strength == 2:
                is_medium_pass = True
            elif pass_strength == 3:
                is_strong_pass = True

        if is_weak_pass == True:
            print("Weak")
        elif is_medium_pass == True:
            print("Medium")
        elif is_strong_pass == True:
            print("Strong")


password_validator()
question from:https://stackoverflow.com/questions/65930228/how-to-escape-iterated-loop-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is a lot of redundancy and reassigning of variables within multiple loops which makes your code a little difficult to read (at least to me).

A couple of pointers to start:

if element.count(x):
    num_count = 2
    num_pass_strength = 1
    if num_count == 2:
        num_pass_strength = 1

Here you you define num_count = 2 and define num_pass_strength = 1, then immediately after run a test to see if num_count == 2 only to define num_pass_strength = 1 again.

You have similar logic lines throughout the code.

This block can be simplified in two ways, depending on what you want to do:

if pass_strength == 1:
    is_weak_pass = True
elif pass_strength == 2:
    is_medium_pass = True
elif pass_strength == 3:
    is_strong_pass = True

if is_weak_pass == True:
    print("Weak")
elif is_medium_pass == True:
    print("Medium")
elif is_strong_pass == True:
    print("Strong")

First, instead of defining is_*_pass to Boolean, only to test it once, you can just move the print statements into the if statements (I will show below). Second, as a python syntax tip in general, if testing a variable that is defined as a Boolean, you do not have to use if variable == True:, if variable: will return the Boolean value of the variable.

Another thing to keep in mind, especially when using open ended variables (i.e. there can be less than one or more than three numbers/special characters in the password), is to use <= and >= to ensure those extremes are accounted for.

Here is an edited version of your code that removes the redundancy but (I think) preserves your overall goal. There are other, probably more efficient ways to do this, this is just a reworking of your code with a similar style in mind. The change of num_list and spec_chars from lists of strings to strings is just a style choice, and has no effect on the outcome of the function.

It can be edited with different and/or statements if you want to determine strength of the password based on the number count and special character count separately.

def password_validator():
    password = input("Please input your password >>> ")

    num_list = '1234567890'
    spec_chars = '!@#$%&*'
    num_count = 0
    spec_count = 0
    for element in password:
        if element in num_list:
            num_count += 1
        if element in spec_chars:
            spec_count += 1

    pass_strength = num_count + spec_count

    if pass_strength <= 1:
        print("Weak")
    elif pass_strength == 2:
        print("Medium")
    elif pass_strength >= 3:
        print("Strong")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...