This Python exercise will involve implementing a mini log-in system. You will download the skeleton of the program, then implement the functions and write the unit tests. The design of the program has been set up for you. In this system, users will be able to sign up, log in, change their password, delete their accounts, and display general statistics about sign-ups and log-ins. The program will initially load a collection of usernames and passwords from a .csv file and store them in a dictionary.
-
Complete the required functions
-
Implement all of the functions defined in the login_system.py
-
Docstrings have already been provided
-
You can create any number of helper functions (with docstrings)
-
Add brief comments to all non-trivial lines of code (dont spendtoo much time on this)
def init_db(file): Loads the given .csv file containing user credentials. Each row is a comma-separated list including username and password, Example(s): lbrandon, My_Crazy_Password_1234 tjones, 4er 3ywort5R dennisq,0987poiu1234QwEr Stores the usernames and passwords in a user's dictionary where the username is the key and the password is the value. Creates an empty logged_in dictionary for storing user log-ins where the username is the key and the value is a bool indicating if the user is logged in or not. users - {} Logged_in - {} return users, logged_in def check_username_password(users, username, password): Checks whether the username exists in the users dictionary and that the password matches the username. Returns boolean. pass def is_valid_password(password): Checks whether the given password is valid. Returns boolean. The length of a valid password should be at least 8 characters and it should contain at least one lowercase character, one uppercase character, and one number, pass def sign_up(users, logged_in, username, password): Allows users to sign up. If the username already exists in the users dictionary, prints a friendly message. If the password does not satisfy the rule(s) (not valid), prints a friendly message. Otherwise, saves the username and the corresponding password in the users dictionary, and prints a success message. Note(s): The user is not automatically logged in when he/she signs up. pass def log_in(users, logged_in, username, password): Allows users to log in. If the username does not exist in the users dictionary or the password is incorrect, prints an error message. Otherwise, saves the username and the value of True in the logged_in dictionary, and prints a welcome message. Note(s): Even if a user is already logged in, he/she can log in again. pass def change_password(users, username, old_password, new_password): Allows users to change their password. If the username does not exist in the user's dictionary, prints an error message. If the old_password is incorrect, prints an error message. If the new-password does not satisfy the rule(s) (not valid), prints an error message. Otherwise, changes the user's password in the user's database, and prints a success message. pass def delete_account (users, logged_in, username, password): Allows users to delete their account. If the username does not exist in the users database, prints an error message. If the old password is incorrect, prints an error message. Otherwise, deletes the user's account from the users dictionary, and prints a success message. Note(s): Also deletes the user's information in the logged_in dictionary. pass def get_sign_ups(users): Returns a list of users who are signed up (in the users dictionary). pass def get_log_ins(logged_in): Returns a list of users who are logged in (in the logged_in dictionary). pass def write_users_db(users, file): Writes all usernames and passwords in the users dictionary, to the given file. Each row is a comma-separated list including username and password. Example(s): lbrandon, My_Crazy_Password_1234 tjones, 4er 3ywort5R dennisq,0987poiu1234QwEr pass def main(): #create database of users and empty database of logged in users users, logged_in = init_db('users.csv') while True: #print options print("Options: " "Press 1 to log in " + "Press 2 to sign up " + "Press 3 to change password " + "Press 4 to delete account " + "Press 5 to show statistics (sign ups and log ins) " + "Press 6 to save all sign ups to the file " "Press 0 to quit ") #get user input option_input = input # try to cast as int try: option = int(option_input) # catch ValueError except ValueError: print("Invalid option.") else: if option == 0: #quit pass elif option == 1: #Log in pass elif option == 2: #sign up pass elif option == 3: #change password pass elif option == 4: #delete account pass elif option == 5: #show statistics pass elif option == 6: #get user input option_input = input # try to cast as int try: option = int(option_input) # catch ValueError except ValueError: print("Invalid option.") else: if option == 0: #quit pass elif option == 1: #Log in pass elif option == 2: #sign up pass elif option == 3: #change password pass elif option == 4: #delete account pass elif option == 5: #show statistics pass elif option == 6: # save user credentials to file pass print("Saved!") print("=" * 80) if __name__ == "__main__": main()