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()
Advertisement
Answer
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