'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 1:[1]
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
Solution 2:[2]
The accepted answer is more than sufficient... but I wrote this so here it is anyways as another reference :')
This will print ALL applicable error messages.
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 check_values(P):
digit_found, upper_found, lower_found = False, False, False
for letter in P:
if letter.isdigit():
digit_found = True
elif letter.isupper():
upper_found = True
elif letter.islower():
lower_found = True
return digit_found, upper_found, lower_found
def checkPassword(Pass):
valid_pass = True
if len(Pass) < 8:
print("Invalid Password: Should be at least 8 characters")
valid_pass = False
digit, upper, lower = check_values(Pass)
if not digit:
print("Invalid Password: Should contain at least one digit")
valid_pass = False
if not upper:
print("Invalid Password: Should contain at least one uppercase character")
valid_pass = False
if not lower:
print("Invalid Password: Should contain at least one lowercase character")
valid_pass = False
return valid_pass
main()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | BeRT2me |
