Programowanie z Pythonem-Zadanie 2 Write a program to check the validity of password input by users. -At least 1 letter between [a-z] and 1 letter between [A-Z]. -At least 1 number between [0-9]. -At least 1 character from [$#@]. -Minimum length 6 characters. -Maximum length 16 characters.
Expected output:
What is your password? Password1 Invalid password format. or: What is your password? P@ssw0rd Registration successful.
Odpowiedź:
import re
while True:
user_input = input("What is your password: ")
check_1 = len(re.findall('[a-z]',user_input))
check_2 = len(re.findall('[A-Z]',user_input))
check_3 = len(re.findall('[0-9]',user_input))
check_4 = len(re.findall('[[email protected]#$%^&*?]',user_input))
check_5 = len(user_input)
validation = [check_1, check_2,check_3,check_4]
if (all(validation) & (5 < check_5 < 17))== True:
print('Registration successful')
break
else:
print('Opps, Invalid password format, try again.')
Wyjaśnienie:
check_1-4 sprawdzanie czy dane znaki zawiera nasze hasło,
check_5 sprawdza długość naszego hasła,
validation = lista checków od 1 do 4, wywołując na niej funkcję all() zwraca True gdy będzie każdy check poprawny.