In: Computer Science
Python Coding
1. validate_username_password(username, password, users): This function checks if a given username and password matches in the stored users dictionary. This function returns True if a match found otherwise returns False.
2. validate_existing_user(users): This function asks for username and password and checks if user provided name and password matches. It prints an informational message and returns username if it does find a match. Call validate_username_password() function to perform this validation. A user has total of three chances to validate. After trying three times the function will print another error message and returns False.
Python code
def validate_username_password(username,password,users):
if username in users.keys():
if users[username]==password:
return True
return False
def validate_existing_user(users):
userchances=3
i=0
while i<3:
usernameinput=input("Enter username:
")
passwordinput=input("Enter password: ")
if
validate_username_password(usernameinput,passwordinput,users)==True:
print("User exists.")
return usernameinput
i=i+1
print("Invalid user.")
return False
usersdict={"user1":"passwdabc","user2":"passwdpqr"}
print(validate_username_password("user1","passwdabc",
usersdict))
print(validate_username_password("userx","passwdxxx",
usersdict))
print(validate_username_password("user1","passwd",
usersdict))
print(validate_existing_user(usersdict))
Sample output 1// check validate conditions
True
False
False
Enter username: user2
Enter password: passwdpqr
User exists.
user2
Sample output 2 //check no of validations
True
False
False
Enter username: adfds
Enter password: sdffdsf
Enter username: jfsdf
Enter password: sfddsf
Enter username: user1
Enter password: passwdabc
User exists.
user1
Sample output 3 //display error is invalid
True
False
False
Enter username: fsdfsd
Enter password: sfds
Enter username: dfdfs
Enter password: sdf
Enter username: sdfs
Enter password: sfds
Invalid user.
False