In: Computer Science
Write a program that validates passwords based on the following
requirements:
• must be at least 8 characters in length
• must include at least 1 alphabet character
• must include at least 1 number
The program should implement the password checker using a function
name validate_password,
which takes two strings as input and returns one of the
following:
• 0 if the passwords match and fulfill all requirements
• 1 if the passwords are not the same length
• 2 if the a number is not found
• 3 if an alphabet character is not found
In main, test your function validate_password by creating 4 sets of
passwords and which match one of the conditions listed above and
printing the result.
Need it in 10 minutes, please.
Code written in python:
def validate_password(password1, password2):
if len(password1) != len(password2) and len(password1) >= 8: # checks the length of two passwords
return 1
elif password1.isalpha(): #checks if the password is not only alphabets that does it contain number or not
return 2
elif password1.isnumeric(): #checks if the password is not only numeric that is does it contain alphabet or not
return 3
else: #for correct password
return 0
if __name__ == '__main__':
res = validate_password("qwer45678", "qwer45678") #perfect password
print(res)
res = validate_password("qwer4567", "qwer45678") # difference in length
print(res)
res = validate_password("qwertyuv", "qwertyuv") # only alphabets
print(res)
res = validate_password("12345678", "12345678") # only numbers
print(res)
Output:
len() helps in verifying length of both passwords
isalpha() helps in checking that the number password contains only alphabet i.e it will be true when the password contains only alphabet and false if the password is alphanumeric
isnumeric() helps in checking that the number password contains only numbers i.e it will be true when the password contains only numbers and false if the password is alphanumeric
So in both isalpha() and isnumeric() if value is True then we return 2 and 3 and in the end else we will return 0