Adding Unique Entries¶
Create a program that will read in a JSON database file, then add an entry using user input, the overwrite the file saving that new entry.
The database is a single dictionary composed of student dictionaries. A dict of dicts. Each key in the database dictionary is a student_id
and the value is a student dictionary.
Once loaded into Python it would look like:
students = {
"188759": {
"name": "Neli Kustner",
"grade": 12,
"student_id": 188759,
"points": 1878
},
"105277": {
"name": "Carolien Amador",
"grade": 12,
"student_id": 105277,
"points": 1416
}
}
What you should do¶
Create a
main
function along with anif __name__ == "__main__"
guard.Ask the user to enter a new student. Be sure to include
name
,grade
,student_id
,points
.Write the new database dictionary to the original file.
Create a function called
is_unique
. It will take a dictionary (the database) as well as astudent_id
. The purpose is to loop through the database dictionary’s.keys()
and returnTrue
if the givenstudent_id
is not in the database already. Essentially, if the id is unique, it returnsTrue
. Write a docstring for this function.Use the
is_unique
function and disallow any user input that contains a duplicatestudent_id
. More points for using the function in a validation loop. Keep the user trapped in the validation loop until they enter a usablestudent_id
.Add a new student student_id: 12345 Error: Student ID already in database. student_id: 12345 Error: Student ID already in database. student_id: 12345 Error: Student ID already in database. student_id: 99999 name: Frank McGill ...
You can use the following file that is a JSON dictionary of student dictionaries referenced by their student ID.
©2021 Daniel Gallo
This work is licensed under Attribution-NonCommercial-ShareAlike 4.0 International