-2

I have been struggling with a task to check if a password has different attributes without for loop. Such as if it has lower case letters, upper case letters, numbers, symbols and so on. would really appreciate some help on the matter. I am new to recursive functions so I would be more than pleased if someone has an idea for a solution not involving them in python.

My attempt so far:

def strength(password):
    if password[1:].isnumeric:
        score = 0
    if password[1:].isalpha():
        score += 3
    if password[1:].islower():
        score += 2
    if password[1:].isupper():
        score += 2
    if password[1:].isalpha():
        score += 3
    return score

Sorry that I didn't put this earlier, I'm still a little new to the site. This code only checks whether the entire password is numeric or lowercase or so on to my understanding. How can I extend this to check for other criteria, such as containing symbols?

Dillon Davis
  • 6,679
  • 2
  • 15
  • 37
ron zamir
  • 23
  • 6
  • 2
    have you tried anything yourself? as it stands, this question is likely to be closed because you haven't shown any attempts to solve the problem and there's no code in your question. – wpercy Mar 14 '19 at 19:33
  • Are you allowed to use while loops? – Hoog Mar 14 '19 at 19:35
  • if 'a' in list(mystring) ? much better, look for regex (regular expressions). Or how password strings are checked, which seems similar – B. Go Mar 14 '19 at 19:40
  • 2
    Look at this https://stackoverflow.com/q/5142103/3350428. – Andrei Odegov Mar 14 '19 at 19:42

2 Answers2

1

You can create functions to check on each of those attributes (some of those already exist for Python's str object but it's a good exercise). The any operator will be your friend here:

def contains_upper(string):
    uppers = 'ACBDEFGHIJKLMNOPQRSTUVWXYZ'
    return any(s in uppers for s in string)

def contains_lower(string):
    # etc... you should implement functions to check other attributes

Now create another function to assess if a given string pass all those tests:

def is_valid_password(string):
    if not contains_upper(string):
        return False
    if not contains_lower(string):
        return False
    # do this for all attributes you want to check
    return True
jfaccioni
  • 7,099
  • 1
  • 9
  • 25
0

Using regular expressions:

import re
regexp = re.compile(r'[a-z]')
if regexp.search(mystring):
    print("lower case found")

Then same with [A-Z] and so on

B. Go
  • 1,436
  • 4
  • 15
  • 22