0

I am new to Python and I am trying to create a program to validate passwords using a for loop (I have written one using a while loop already. However it is not working correctly.

Like I said I am very new to Python and code writing. I am finding especially difficult looping through the values of a string (password entered) and resolving how to deal with exceptions (for example, not containing certain characters) This is the code I have written.

import string
import re

print('At least 1 letter between [a-z] and 1 letter between [A-Z]. \nAt least 1 number between [0-9]. \nAt least 1 character from [$#@]. \nMinimum length 6 characters. \nMaximum length 16 characters. \n')

def validate():
    password = input('Enter your password: \n')
    if (len(password) < 8):
        print('Password must be at least 8 characters long')
    elif (len(password) >= 16):
        print('Password must be no more than 16 characteres long')
    for i in password:
        if (i.find(string.ascii_lowercase)):
            print('At least one character must be in the range [a-z]')
        elif (i.find(string.ascii_uppercase)):
            print('At least one character must be in the range [A-Z]')
        elif (i.find(string.digits)):
            print('At least one character must be in range[0-9]')
        elif (re.search('[@,#,$]', password) is None):
            print('At least one of these characters (@ - # -$) must be included')
        else:
            print('your password is good:')
            break
validate()
2
  • 3
    What is the purpose of the for loop here? i would recommend you really think about the logic of your if/else logic because it will not work. When you do for i in password, you are iterating each character in the password as i. I would highly recommend drawing a flow diagram and working from there. Commented May 11, 2020 at 11:21
  • Basically all 4 regexes should match... [a-z]+ [A-Z]+ [0-9]+ [@#$]+ Commented May 11, 2020 at 11:48

2 Answers 2

1

Try this:

import re
if re.search("[a-z]+", password) is None:
    print('At least one character must be in the range [a-z]')

if re.search("[A-Z]+", password) is None:
    print('At least one character must be in the range [A-Z]')

if re.search("[0-9]+", password) is None:
    print('At least one character must be in the range [0-9]')

if re.search("[@#$]+", password) is None:
    print('At least one character must be in the range [a@#$]')
Sign up to request clarification or add additional context in comments.

Comments

0

I wanted to use the for loop as an exercise. Just to practise the use of it.

Thanks for the advice. I am very new to all this and I make some very shocking mistakes.

I would still like some pointers about how to iterate through the values of a string and handling the occurrence (or not) of certain characters in that string.

Again, many thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.