1

I'm struggling with my password rater program. How can i make sure that when i insert a password, my program returns the password strength. When i run this program it says that password_strength is stored at .....) is the return that i call at the end of my program wrong?

print ("Welcome to this password rater program")
print ("Your password needs to have a minimum of 6 characters with a maximum of 20 characters.")
print ("Your password can contain lowercase and uppercase characters, numbers and special characters")
print("")


password = input("Please insert your password: ")


def check_len(password):
    l = len(password)
    if 6 < l < 20:
        x = check_char(password, l)
    else:
        x =  0
    return x


def check_char(password, l):
    for i in range(l):
        ascii = ord(password[i])
        num = 0                                
        upper = 0                               
        symbols = 0
        lower = 0
        space = 0

        if  96 < ascii < 123:      
            lower = 15
        elif 47 < ascii < 58:     
            num = 25
        elif 64 < ascii < 91:     
            upper = 25
        elif ascii == 32:
            space = 30
        else:                               
            symbols = 25
        total = ((lower + num + upper + space + symbols) * len(password))
    return total


def password_strength(total):
    if total >= 1000:
        print("Your chosen password is Very strong")
    elif 700 < total < 999:
        print("Your chosen password is strong")
    elif 500 < total < 699:
        print("your chosen password is medium")
    elif total <= 500:
        print("Your chosen password is weak")
    return total


strength = check_len(password)
print(password_strength(strength))
  • 1
    You are printing the reference to the function. It should be `print(password_strength(total))` where `total` is the number returned by the function `check_char()` – Farhan.K Oct 07 '16 at 09:22
  • 1
    What is `for i in range(l)` this doing? You cannot access `l` from `check_char()`, I suggest you read first of all this http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules You should call `check_len` from within `check_char`, and `password` should be a parameter of `check_char` – Antonio Oct 07 '16 at 09:27
  • 2
    The errors in your code are 1)`check_char` takes no input but `check_len` provides it with two inputs `(password,l)` 2) `check_len` is not returning anything 3) `check_char` is not called and hence expecting any return from it is not appropriate. 4) There is a flaw in you print statement. I believe you have to do something like `print(password_strength(check_len(input)))` – Prasanna Oct 07 '16 at 09:31
  • Your `check_len` returns None, it should return `x`. I suggest combining `check_len` and `check_char` into a single `check_password` function. Also, several of your `<` comparisons should be `<=`. Your code would be more readable if you tested directly against the characters, rather than using `ord()`, eg: `if 'a' <= ch <= 'z':`. – PM 2Ring Oct 07 '16 at 09:33
  • @Antonio Well he calls the `check_char()` from the `check_len()` and not from anywhere else. Hence the supposition :) – Prasanna Oct 08 '16 at 16:58

2 Answers2

3

Well first of all, you tell python to print the function, not the evaluation of the function. This is why you get that message.

Also, you never call any of the functions you wrote. To get a working program after the declaration of all definitions is the following:

Call the check_len definition:

strength = check_len(password)

This definition doesn't return any value though. You could change it to:

def check_len(password):
    l = len(password)
    if 6 < l < 16:
        x = check_char(password, l)
    else:
        x =  0
    return x

To have it return the score/strength.

Finally you should process the score using your 'password_strength' definition:

password_strength(strength)

In this line there is no need to print, because the printing statements are in the definition itself. If you want to print the final score as well though, you could make it into the following line:

 print(password_strength(strength))

One more debug thingy: your check_char definition doesn't take any arguments. You could solve this by changing it into:

def check_char(password, l):

Final code: print ("Welcome to this password rater program") print ("Your password needs to have a minimum of 6 characters with a maximum of 16 characters.") print ("Your password can contain lowercase and uppercase characters, numbers and special characters") print ("")

password = raw_input("Please insert your password: ")


def check_len(password):
    l = len(password)
    if 6 < l < 16:
        x = check_char(password, l)
    else:
        x =  0
    return x


def check_char(password, l):
    for i in range(l):
        ascii = ord(password[i])
        num = 0                                
        upper = 0                               
        symbols = 0
        lower = 0
        space = 0

        if  96 < ascii < 123:      
            lower = 15
        elif 47 < ascii < 58:     
            num = 25
        elif 64 < ascii < 91:     
            upper = 25
        elif ascii == 32:
            space = 30
        else:                               
            symbols = 25
        total = ((lower + num + upper + space + symbols) * len(password))
    return total


def password_strength(total):
    if total >= 1000:
        print("Your chosen password is Very strong")
    elif 700 < total < 999:
        print("Your chosen password is strong")
    elif 500 < total < 699:
        print("your chosen password is medium")
    elif total <= 500:
        print("Your chosen password is weak")
    return total


strength = check_len(password)
print(password_strength(strength))     `
Michel Touw
  • 626
  • 6
  • 15
  • i changed my check_len function to include the return x and adjusted the parameters for check_char into (password, l). i removed the print statement so i have just return total. but when i run the program is still does not give me the print statement for the result? what a i doing wrong – Edwin Oliveira Oct 07 '16 at 09:53
  • The check_len in the edited code doesn't return x yet, but i'll assume you did that on your computer. You still need to call these functions though by putting: `print(password_strength(strength)) ` on the last line – Michel Touw Oct 07 '16 at 09:59
  • hey michel, my code now says that strength is not defined. this is because i put it inside the check_len function right? – Edwin Oliveira Oct 07 '16 at 10:13
  • 1 more thing to do: move `strength = check_len(password)` to the second last line. I just edited my answer with an example of working code – Michel Touw Oct 07 '16 at 10:22
  • Only one more little thing, the code is up and running but whenever i insert a random of characters and it meets the criteria it should do for example: Brunno1996 is the same as 60 points it then should do 60*len password which is 10 and it should be 600 points but when i run the program it says 25 points or sometimes 200? how can i fix this – Edwin Oliveira Oct 07 '16 at 11:09
  • if you want the numbers to add up (so lowercase is +15 and uppercase adds an additional 25) then you should change the `elif` into `if` cases. Now it will stop at the first case that is `True` and ignore the rest – Michel Touw Oct 07 '16 at 11:13
  • one last question and my program will be perfect, ive changed my program with if statements nd it works but how can i change the else statement that i use for any other character in the ASCII tabel to represent symbols? do i have to make if statements for each one of them or can i do in in a more easy way? – Edwin Oliveira Oct 07 '16 at 11:33
  • you could exclude any of the know ascii numbers – Michel Touw Oct 07 '16 at 12:13
1

You never call your python functions.

def potatis():
    print("Hello yu!")

defines a function, but you also need to call the function potatis() for the code inside to actually run.

Moberg
  • 5,253
  • 4
  • 38
  • 54