Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

an alternative to (any) function

I’m currently working on a password validity code, but I didn’t learn the (any) function yet so I should not use it
I’m looking for a different way to make the code work and check if there is lowercase, uppercase, and digits in the password. I thought about using a loop that checks each character, but I couldn’t do it since I’m still learning loops

def main():
    P1 = str(input("Enter a password: "))
    if checkPassword(P1) == True:
        P2 = str(input("Confirm the Password: "))
        if P1 != P2:
            print("Password does not match!")
            main()
        else:
            print("The password is valid!")


def checkPassword(Pass):
    if len(Pass) < 8:
        print("Invalid Password: Should be at least 8 characters")
        return False
    if not any(char.isdigit() for char in Pass):
        print("Invalid Password: Should contain at least one digit")
        return False
    if not any(char.isupper() for char in Pass):
        print("Invalid Password: Should contain at least one uppercase character")
        return False
    if not any(char.islower() for char in Pass):
        print("Invalid Password: Should contain at least one lowercase character")
        return False
    return True

main()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You could just make your own any function and then use that if you’d prefer,

def my_any(iterable, predicate):
    for item in iterable:
        if predicate(item):
              return True
    return False


if not my_any(Pass, lambda char: char.isdigit()):
    print("Invalid Password: Should contain at least one digit")
    return False

without a lambda, you need to figure out what errors have been triggered. and then use your if statement after that

digit_found = False
lower_found = False
upper_found = False
for char in Pass:
     if char.isdigit():
          digit_found = True
     elif char.isupper():
          upper_found = True

if not digit_found:
    print("Invalid Password: Should contain at least one digit")
    return False
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading