0

I need help to solve a code to check for valid password and condition are:

  1. The password should be of at least eight characters.

  2. It is a combination of alphabets, numeric digits 0 to 9, and special characters '_', '@', '#', and '*'. No other character is allowed.

  3. It should contain one capital alphabet, one special character from ('_', '@', '#', '*'), and one numeric digit from 0 to 9.

temp_list=['_','@', '#','*']

flag=False

for a in range (6):
    try:
        pas=list(input("enter password :"))
    except ValueError:
        continue
    if len(pas)<8:
        print("try again")
        continue
    elif len(pas)>8:
            for j in range(10):
                for t in temp_list:
                    if str(j) in pas and t in pas:
                        flag=True
                        continue
                    else:
                        flag=False
                        break
    if a<5:
        if str(j) not in pas or t not in pas:
            print("J: ",j," T: ", t)
            print("try again")
            continue
    else:
        break
if flag==True:
    print("Valid password")

in this code it does not print valid when the password entered is valid.

DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
  • 1
    This is pretty messy. I see no check for upper case. In the first loop you seem to have mixed up `break` and `continue`. See if you can [edit] this into a [mre]. – tripleee Sep 08 '19 at 12:57
  • 1
    We have a cubic light year of password validation questions here; did you look at some old ones? – tripleee Sep 08 '19 at 12:58
  • E.g., here https://stackoverflow.com/questions/53553464/password-validation-in-python – AnsFourtyTwo Sep 08 '19 at 13:03
  • I'd suggest you extract a [mcve] as well. Also, consider unit testing your function, so that changes to fix one case don't break earlier working cases. As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Sep 08 '19 at 13:30

2 Answers2

1

A quick fix would be replacing the elif part with:

[...]
    elif:
        for j in range(10):
            for t in temp_list:
                if str(j) in pas and t in pas:
                    flag = True
        if flag == False: # no special characters/numbers found
            print("try again")
            continue
        else:
            break
[...]

so that you check your flag after you loop through the possibilities of special characters and numbers.

But this code is still missing the check for capital letters, as mentioned in the comments. Check out this question to see how to do this in python.

pguenther
  • 112
  • 8
0

If you want to use RegExr version of validation check this hope it help you:

        import re

        temp_list = ['_', '@', '#', '*']


        def check_password(psw):
            """ make sure psw respect rules

            1. It is a combination of alphabets, numeric digits 0 to 9,
            and special characters '_', '@', '#', and '*'. No other character is allowed.

            2.It should contain one capital alphabet, one special character
            from ('_', '@', '#', '*'), and one numeric digit from 0 to 9.
            """
            if not re.match(r'^([0-9]|[a-zA-Z]|_|@|#|\*)+$', psw):
                return False, """It is a combination of alphabets, numeric digits 0 to 9, and special characters '_', '@', '#', and '*'. No other character is allowed"""
            if not re.match(r'.*?[A-Z].*', psw):
                return False, 'Password should contains at least one capital alphabet'
            if not re.match(r'.*?[_|@|#|\*].*', psw):
                return False, 'Password should contains at least one special character from (%s)' % ','.join(temp_list)
            return True, 'Valid password'


        valid = False
        for a in range (6):
            try:
                pas = input("enter password :")
            except ValueError:
                continue
            if len(pas) < 8:
                print("Password should contains at least 8 letters try again.")
            valid, msg = check_password(pas)
            if not valid:
                print(msg, 'try again.')
            else:
                valid = True
                break  # pass word is valid
        else:
            print('You all ready tried 6 times sorry!')
            not_valid = True

        if valid:
            # do you work here
Charif DZ
  • 14,415
  • 3
  • 21
  • 40