The program must also ask for a user to enter it and then confirm it:
Enter your password:
Re-enter your password:
If the passwords don’t match or the rules are not fulfilled, a message is printed as to what the error is and the user is prompted again. Your program must include a function that checks whether the rules are passed and if the password is valid (returns True if it is valid):
defisValidPassword(password)
Note: Re-enter password AFTER you validate the password first
Test Cases:
Abcdefgh – (should print “must have at least one digit”)
1BCDEFGH – (should print “must have at least one lower case”)
1abcdefgh – (should print “must have at least one upper case”)
1Abcdef – (should print “must be at least 8 characters long”)
1Abcdefg – (Valid: should ask to Re-enter password)
Python, keep it simple
(I have provided the code with and without line comments and explained each line in line comments so that there will be no confusion for you. Please go through the code which you are comfortable in)
(Please open the screenshots in a seperate tab so that they are visible clearly.)
(If you still have any doubts regarding this answer please comment. I will definitely help. Thank you)
Code with line comments:
#validation function definition def isValidPassword(passwd): #find length of password n = len(passwd) #initialize d, l,u to 0 d = 0 l = 0 u = 0 #find number of lower chars upper chars and digits in the password for i in passwd: if i.isdigit(): d = d + 1 if i.islower(): l = l + 1 if i.isupper(): u = u + 1 #check whether n<8 if yes print password must be 8 chars and #ask user to enter password again #for this we can just call main() function by which the user enters password again if (n < 8): print("password must be at least 8 characters long") main() #else check whether password has less than 1 lower char #if yes print password must have atleast one lower case and call main() function elif (l < 1): print("password must have at least one lower case") main() # else check whether password has less than 1 upper char # if yes print password must have atleast one upper case and call main() function elif (u < 1): print("password must have at least one upper case") main() # else check whether password has less than 1 digit # if yes print password must have atleast one digit and call main() function elif (d < 1): print("password must have at least one digit") main() #else return true. # If all the above conditions fails.This condition executes and returns True else: return (True) # main function def main(): #initially declare two strings passwd and re_pass passwd = "" re_pass = "" #input password passwd = input("Enter your password: ") #call the isValidPassword() function to validate the password validation_result = isValidPassword(passwd) #if the function returns true then print password is valid and #ask user to re type the password and print congrats message if (validation_result == True): print("Your password is Valid: ") print("Please Re-enter your password to confirm it") Re_pass = input() print("Congrats you created your password successfully") if __name__ == "__main__": main()
Code without line comments:
def isValidPassword(passwd): n = len(passwd) d = 0 l = 0 u = 0 for i in passwd: if i.isdigit(): d = d + 1 if i.islower(): l = l + 1 if i.isupper(): u = u + 1 if (n < 8): print("password must be at least 8 characters long") main() elif (l < 1): print("password must have at least one lower case") main() elif (u < 1): print("password must have at least one upper case") main() elif (d < 1): print("password must have at least one digit") main() else: return (True) def main(): passwd = "" re_pass = "" passwd = input("Enter your password: ") validation_result = isValidPassword(passwd) if (validation_result == True): print("Your password is Valid: ") print("Please Re-enter your password to confirm it") Re_pass = input() print("Congrats you created your password successfully") if __name__ == "__main__": main()
Sample Testcase 1:
Enter your password: Abcdefgh
password must have at least one digit
Enter your password: 1BCDEFGH
password must have at least one lower case
Enter your password: 1abcdefgh
password must have at least one upper case
Enter your password: 1Abcdef
password must be at least 8 characters long
Enter your password: 1Abcdefg
Your password is Valid:
Please Re-enter your password to confirm it
1Abcdefg
Congrats you created your password successfully
Sample Testcase2:
Enter your password: jack123
password must be at least 8 characters long
Enter your password: jack123@
password must have at least one upper case
Enter your password: Jack123@
Your password is Valid:
Please Re-enter your password to confirm it
Jack123@
Congrats you created your password successfully
Code Screenshot with line comments:
Code Screenshot without line comments:
Sample Testcase1 Screenshot:
Sample Testcase2 Screenshot:
Get Answers For Free
Most questions answered within 1 hours.