0

I am writing a program that is supposed to take a string (the password entered by user) and test it to make sure it meets the following requirements: must contain at least one uppercase letter and one lowercase letter must start with a letter minimum of eight characters no blanks must contain at least two digits

this is what i have so far and I'm stuck getting invalid syntax errors running = True

while running:
    valid = 0
    password = str("Enter Password: ")
    if len(p) <8:
        print ("The password you entered is too short. Please try again.")
        running = False
    import re

    #check if contains a digit
    if re.search(r '\d', password):
        password = valid

    #check if contains uppercase letter
    if re.search(r '[A-Z]', password):
         password = valid

    #check if contains lowercase letter
    if re.search(r '[a-z]', password):
        password = valid

    #check if contains only letters
    if re.search(r "[a-z]", password) and re.search(r "[A-Z]", password):
        print ("Password must contain at least 2 digits. Please try again.")

    #check if contains all lowercase letters
    if password.islower():
        print ("Password must contain at least 1 uppercase letter. Please try again.")

    #check if contains all uppercase letters
    if password.isupper():
        print ("Password must contain at least 1 lowercase letter. Please try again.")

    if password == valid:
        print ("Valid Password")
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
user3060027
  • 1
  • 1
  • 1
  • the logic here doesnt work at all -- once you pass the first (digit) check you set password equal to "valid" (which is zero..). You will then fail all the other tests (throw an error actually i think since re.search expects a string. – agentp Dec 03 '13 at 05:38
  • This is a dup of dozens of questions here. Did you try searching at all? In addition to the one I linked, see [here](http://stackoverflow.com/questions/17105957/python-password-strength?rq=1) and [here](http://stackoverflow.com/questions/16709638/checking-the-strength-of-a-password-how-to-check-conditions), and many of the related questions on those 3, and everything that comes up in a search for `[python] [passwords]`, and… – abarnert Dec 03 '13 at 05:46
  • you think "make a password checker" is homework exercise 1 in chapter 2 of every python textbook.. – agentp Dec 03 '13 at 05:51

2 Answers2

2

There are multiple problems with the code shown, in addition to the spacing error in your regexps, as mentioned by DevEight.

1) password = str("Enter Password: ") - this does not ask the user for a password, this sets the name password to refer to the string "Enter Password: ". You want input instead (or raw_input for Python version 2.x).

2) if len(p) <8: - p is not a defined name. Presumably you mean password.

3) running = False - this is the only place you set this variable, but it controls your while loop. Your loop only exits when this is false, so this will keep looping until the password is too short, whereup it will eventually exit.

4) password = valid - this sets the password name to refer to the contents of the variable valid, which you initialize to 0. You then run further regular expression searches against the integer 0, which are of course wrong.

5) if re.search(r "[a-z]", password) and re.search(r "[A-Z]", password): - this is commented to say that it requries that there be at least two numbers in the password. It doesn't. I have no idea why you might think it does.

6) if password == valid: - this kind of sort of works, in the sense that you may have previously set password to have the same value as valid. As mentioned above, that's wrong, but it means this could sometimes return True. With working code, you need completely different logic.

7) To top it all off, your question refers to syntax errors from running = True - but that assignment isn't present anywhere in the code.

On the up side, good use of isupper and islower. Those methods make your earlier upper and lowercase regexp searches unnecessary.

Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
0

You don't seem to get the password from the user.

password = str("Enter the password")

should be:

password = raw_input("Enter the password")

Then, you are checking for valid conditions, which may cause logical errors. So, instead of checking if it satisfies all conditions, check if it does not satisfy any condition (ie) reverse of what you are doing right now..

Eg)

flag = 1

invalid = 0
if(len(password)<8):
    flag = 0
if password.islower():
    flag = 0
#all conditions
#at last, check for the flag value.
if flag:
    print "Password valid"
else:
    print "Password not valid"
Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69