2

I am new to the world of Python and have been given the task to complete the following:

Design, code, test and evaluate a system to accept and test a password for certain characteristics:

  • It should be at least 6, and no more than 12 characters long.
  • The system must indicate that the password has failed and why, asking the user to re-enter their choice until a successful password is entered.
  • A message to indicate that the password is acceptable must be displayed.
  • Password strength can be assessed against simple criteria to assess its suitability; for example a password system using only upper and lower case alphabetical characters and numeric characters could assess the password strength as:
    • WEAK if only one type used, e.g. all lower case or all numeric
    • MEDIUM if two types are used
    • STRONG if all three types are used.

So far I have done the following but not got it to work correctly:

def password():

    print ('Welcome user ! Please enter password below\n')
    print ('The password entered must be between 6-12 characters long\n')

    while True:
        password = input ('Please enter your password . . . :')
        weak = 'weak'
        med = 'medium'
        strong = 'strong'
        if len(password) >12:
            print ('password is too long It must be between 6 and 12 characters')
        elif len(password) <6:
            print ('password is too short It must be between 6 and 12 characters')
        elif len(password) >=6 and len(password) <= 12:
            print ('password ok\n')
            if password.lower()== password or password.upper()==password or password.isalnum()==password:
                print ('password is', weak)
            elif password.lower()== password and password.upper()==password or password.isalnum()==password and password.upper()==password:
                print ('password is', medium)
            else:
                password.lower()== password and password.upper()==password and password.isalnum()==password
                print ('password is', strong)
            break

password()

I have tried to introduce a while loop:

while invalid:
    if len(password) >=6 and (password) <=12:
        password=False
        # number in range
        # sets invalid to False to stop loop
    else:
        print('Sorry the password you entered was not between 6 and 12 characters long')
        print('Please try again')
print('You have entered a valid password')

But still cant get it to work Please help !!!

Namit Sinha
  • 1,415
  • 3
  • 16
  • 30
Rav
  • 21
  • 1
  • 2

5 Answers5

1

Ok its not clear what exact problem you are facing But your condition to check for password being of strength medium is sloppy

 elif password.lower()== password and password.upper()==password or password.isalnum()==password and password.upper()==password:

Here's a suggestion consider Boolean variable

D ->string containing at least 1 digit

U -> string Containing at least 1 upper case and

L->string containing atleast 1 lower case character

D | U | L == low | medium | strong 

0   0   0     1
0   0   1     1 
0   1   0     1
0   1   1           1     
1   0   0     1
1   0   1           1
1   1   0           1
1   1   1                     1

there is only one way a password can be considered strong

you can compute D by using regex

_digits = re.compile('\d')
def contains_digits(d):
    return bool(_digits.search(d))

conditions U and L can be computed easily

now after reducing your expression

strong = D * U * L

medium = (!D * U * L) + (D * !U * L) + (D * U * !L)

low = (!D * !U ) + (!D * !L) + (!U * !L)

so your code will look like

D = areDigits(Password)
U = areUpper(Password)
L = areLower(Password)


if((!D and !U) or (!D and !L) or (!U and !L)):
    print("weak password")
elif(D and U and L):
    print("strong password")
else:
    print("medium strength password")

This may looks slightly ugly but its a much more systematic way to handle this Think what would you do if you were to include special characters and other requirements !

Community
  • 1
  • 1
Namit Sinha
  • 1,415
  • 3
  • 16
  • 30
0

Try checking your password against various conditions using regular expression to search and match the password.. You may find this link useful:

Password checker containing many conditional statements

Community
  • 1
  • 1
user958119
  • 63
  • 6
  • It doesn't quite work, does it? One problem is that `password()` never returns anything... – Tim Pietzcker Jul 22 '14 at 08:12
  • hmmmm...I've used this function before and was sure it should work. I run it but once I enter the password I get `NameError: name 'dl45g7' is not defined`.. I'll check what's wrong and edit my answer.. Thanks – user958119 Jul 22 '14 at 08:25
0

Agreed this is a good use case for regular expressions.

Also It seems like your medium case will never be triggered:

elif password.lower()== password and password.upper()==password or password.isalnum()==password and password.upper()==password:

>>> 'a123'.lower() == 'a123' and 'a123'.upper() == 'a123'
False

>>> '1234'.isalnum() and '1234'.upper() == '1234'
True

since lower and upper have to be true at the same time or isalnum and isalupper have to be triggered at the same time -> But A12345 or 12345 is considered weak, so it can't trigger the med...

You might consider making a test suit for your passwords.

Maximilian Kindshofer
  • 2,753
  • 3
  • 22
  • 37
0

Your code is a bit hard to read. I would split the actual tests of the password up into a separate function, like in the program below.

Then you see more clearly what is being tested, and you can react to the results of the tests in the main function.

Im using a password class to store the test results because I like how clean it looks.

Example for Python 3:

import re

def check_password(chars, min_chars=6, max_chars=12):
    class Password: pass

    Password.has_uppercase = bool(re.search(r'[A-Z]', chars))
    Password.has_lowercase = bool(re.search(r'[a-z]', chars))
    Password.has_numbers = bool(re.search(r'[0-9]', chars))

    if not min_chars <= len(chars) <= max_chars:
        print("Password needs to be between %s and %s characters." % (min_chars, max_chars))
        return


     /* Return a list of only my attributes
        of the Password class. */

     return [t for t in vars(Password).items() if not t[0].startswith("__")]



if __name__ == "__main__":
    meanings = {0: "unacceptable", 1: "Weak", 2: "Medium", 3: "Strong"}

    while True:
        chars = input('\nEnter a password: ')
        results = check_password(chars)
        if results:

            /* Points are equal to how many tests are successful */
            points = len([p[0] for p in results if p[1] is True])

            /* Print out the results to the console */
            print("\nPassword strength is %s.\n" % meanings.get(points))

            for condition, result in results:
                print("{0:<15} {1} {2}".format(condition, ":", bool(result)))
            break
Christer Nissen
  • 487
  • 2
  • 6
0
while invalid:
    if len(password) >=6 and (password) <=12:
        password=False
        # number in range
        # sets invalid to False to stop loop
    else:
        print('Sorry the password you entered was not between 6 and 12 characters long')
        print('Please try again')
print('You have entered a valid password')
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364