Fab
Fab

Reputation: 17

Python Password Checker Incorrect Output

I am trying to Implement the function _isValid() in password_checker.py that returns True if the given password string meets the following requirements, and False otherwise:

Here is my code:

import stdio
import sys


# Entry point
def main():
    pwd = sys.argv[1]
    stdio.writeln(_isValid(pwd))


# Returns True if pwd is a valid password, and False otherwise.
def _isValid(pwd):
    check1 = False  # length flag
    check2 = False  # digit flag
    check3 = False  # upper case flag
    check4 = False  # lower case flag
    check5 = False  # alphanumeric flag

    # If pwd is long enough, set corresponding flag to True.
    if len(pwd) >= 8:
        check1 = True

    for c in pwd:
        # For each character c in pwd...

        if c.isdigit():
            # If c is a digit, set corresponding flag to True.
            check2 = True
        elif c.isupper():
            # If c is in upper case, set corresponding flag to True.
            check3 = True
        elif c.islower():
            # If c is in lower case, set corresponding flag to True.
            check4 = True
        elif c.isalnum():
            # If c is not alphanumeric, set corresponding flag to True.
            check5 = True

    # Return True if all flags are True, and False otherwise.
    if check1 and check2 and check3 and check4 and check5:
        return True
    else:
        return False


if __name__ == '__main__':
    main()

I don't get the correct output and can't seem to figure out why

Upvotes: 0

Views: 68

Answers (1)

3ximus
3ximus

Reputation: 400

You have the answer in your own comment:

elif c.isalnum():
   # If c is not alphanumeric, set corresponding flag to True.

Therefore this should be:

elif not c.isalnum():

Upvotes: 1

Related Questions