0

No i'm not cursing in the title. i need to create a password processing program that checks a input on whether it meets certain criteria one of which is that it must contain one of the characters $@!%&*_. this is what i currently have.

def pword():
    global password
    global lower
    global upper
    global integer

password = input("Please enter your password ")
length = len(password)
lower = sum([int(c.islower()) for c in password])
upper = sum([int(c.isupper()) for c in password])
integer = sum([int(c.isdigit()) for c in password])


def length():
    global password
if len(password) < 8:
    print("Your password is too short, please try again")
elif len(password) > 24:
    print("Your password is too long, please try again")

def strength():
    global lower
    global upper
    global integer
if (lower) < 2:
    print("Please use a mixed case password with lower case letters")
elif (upper) < 2:
    print("Please use a mixed case password with UPPER case letters")
elif (integer) < 2:
    print("Please try adding numbers")
else:
    print("Strength Assessed - Your password is ok")
vilty
  • 11
  • 1
  • You can use `re.match(...)` in the `re` module of the standard library. Note that some of the symbols you're matching against have a different meaning in the context of a regular expression, so be sure to escape them -- for example in match use `'\*'` to find a literal `*` vs. `'*'` to mean "0 or more repetitions". https://docs.python.org/3.5/library/re.html#match-objects – Taylor D. Edmiston May 10 '16 at 05:14
  • By the way, you don't need to declare `global foo` for variables inside functions unless you're modifying them -- they already have read access to global per [the rules of scope resolution](http://stackoverflow.com/a/292502/149428). You could remove all of your global decorations inside `length()` and `strength()`. – Taylor D. Edmiston May 10 '16 at 05:22

3 Answers3

0

This kind of thing will work:

required='$@!%&*_'

def has_required(input):
    for char in required:
        if input.contains(char):
            return True
    return False

has_required('Foo')
Jameson
  • 6,400
  • 6
  • 32
  • 53
0
must_have = '$@!%&*_'
if not any(c in must_have for c in password):
    print("Please try adding %s." % must_have)

any(c in must_have for c in password) will return True if any one of the characters in password is also in must_have, in other words, it will return True is the password is good. Because want to test for bad passwords, we put not in front of it to reverse the test. Thus the print statement is executed here only for bad passwords.

John1024
  • 109,961
  • 14
  • 137
  • 171
0

You can achieve this easily with a list comprehension + the any() built-in.

has_symbol = any([symbol in password for symbol in list('$@!%&*_')])

Or a little more broken up:

required_symbols = list('$@!%&*_')
has_symbol = any([symbol in password for symbol in required_symbols])
Taylor D. Edmiston
  • 12,088
  • 6
  • 56
  • 76