1

Individually, these statements work (I've made individual helper functions) and I've compiled them onto one function. How am I able to get them to work together? Additionally, how would I make this program run without module 're'? It works but I got it off someone else on this site. These are what I need in this program:

  • Has to have a number
  • Characters in the beginning and end of the string have to be letters
  • Must be between 10 and 20 characters
  • There can't be 3 characters in a row
  • Previous password can't be used again

Here is my code:

import re 
def password_validator (pw_v, prev_p): #pw_v = string that is to be validated; prev_p = previously used strings in a list

    prev_P = [s]
    # do I use 'while True' instead of returning True statements every time?
    if 10 <= len(s) <=20:
        return True
    elif s[0] and s[-1].isalpha():
        return True 
    elif not re.search('(.)\\1{2}', s): # How else can I write this without using 're'?
        return True
    elif any(digit.isdigit() for digit in s):
        return True
    else: 
        return False
2
  • You can use or's to organize your code a little better. Commented May 9, 2018 at 6:05
  • Note that the return statement means exit from the function, so in this case if any of the condition matches, it will return True. Commented May 9, 2018 at 6:06

3 Answers 3

1

Your code checks if the input satisfies only one of the conditions.

Note that when you return, the function returns and ignores the rest of the code. Considering this fact, you can use either:

(1) Nested ifs

if 10 <= len(s) <= 20:
    if s[0] and s[-1].isalpha():
        # the rest of the conditions
            return True    # all of the conditions were met
return False    # one of the conditions wasn’t met

(2) Return false when one the first condition isn't met (which actually uses De Morgan's laws).

if not 10 <= len(s) <= 20:
    return False
if not s[0] and s[-1].isalpha():
    return False 
# the rest of the conditions

Regarding the regex use, in my opinion it's elegant in this case; but you can always switch to a loop that iterates over the input's characters, combined with a counter of repeated characters (which isn't as elegant):

def three_identical_characters(input):
    counter = 0
    for i in range(1, len(input)):
        counter += (1 if (input[i] == input[i-1]) else 0)
        if counter == 2:
            return True
    return False
Sign up to request clarification or add additional context in comments.

Comments

1

Store each of the conditions results in a variable, e.g. has_correct_length and has_digit.

Combine them:

has_correct_length = (10 <= len(s) <=20)
has_digit = any(digit.isdigit() for digit in s)
fulfills_restrictions = has_correct_length and has_digit

This way your code is much easier to read and documents itself.

Comments

-1

You don't test if the condition is met, you test if the condition is not met:

import re 
def password_validator (pw_v, prev_p): #pw_v = string that is to be validated; prev_p = previously used strings in a list

    prev_P = [s]
    # do I use 'while True' instead of returning True statements every time?
    if not 10 <= len(s) <=20:
        return False
    elif not (s[0] and s[-1].isalpha()):
        return False
    elif re.search('(.)\\1{2}', s): # How else can I write this without using 're'?
        return False
    elif not any(digit.isdigit() for digit in s):
        return False
    else: 
        return True

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.