This HW is focused on creating a login system. There will be a file called login_database.csv
that is a CSV file of all the usernames and passwords in our database. Here is an excerpt of the starting database:
username,password
jane,bats
john,cats
jessica,rats
Here we can see that we have three users in the database. We can read this CSV file as a list of dictionaries by using the following code:
with open("login_database.csv", 'r') as f
list_of_users = list(DictReader(f))
list_of_users
now is a list of dictionaries. You can now loop through every user like you would any other list.
Your objective is to create a program that allows users to:
- Login to the system using their credentials (they will enter their username and password). You will check if its valid.
- Register a new login (user will create a user name and password)
- Quit. The program save the any newly registered users into
login_database.csv
before quitting.
- Create a function called
read_database()
. It should read the filelogin_database.csv
as a list of dictionaries (see above) and return it. (2 pts) - Create a function called
login(list_of_users)
. It should ask the user for the username and then the password. If the username and password exist in the database return True, else return False. (2 pts) - Create a function called
register(list_of_users)
. It should ask the user for a username and password. You should then create a dictionary of this new username and password andappend
tolist_of_users
. (2 pts) - Create a function called
write_database(list_of_users)
. It should overwrite the existinglogin_database.csv
file withlist_of_users
. Please see the documentation (2pt)- HINT - Open the file like this,
with open(DATABASE_FILE, 'w', newline='') as f:
- HINT - Open the file like this,
- Finish the remaining conditional statements in the
main
function while loop. You should check the command name and then call the respective functions (2 pts) - No syntax or run time errors (1 pt)
- Modify the
register
function such that you can not have duplicate user names (1pt).
Example Output
This the running program in VSCodes terminal. If you don't have VSCode setup to use GitBash, you will use Windows Terminal.