1 of 10

Dicts + JSON

2 of 10

revisiting code from yesterday

https://replit.com/@MarkSantolucito/dicts

We want to print the student name alongside each grade

then

We want to calculate the average of each student

3 of 10

revisiting code from yesterday

https://replit.com/@MarkSantolucito/dicts

We want to print the student name alongside each grade

then

We want to calculate the average of each student

Next: let’s make an interactive version where we can input students and grades

4 of 10

Interactive gradebook

Allow user to:

add_student(gradebook, student): adds a student with no grades to the gradebook, if student already exists, do nothing. return the updated gradebook.

add_grade(gradebook, student, assignment, grade): add a grade to that student, return the updated gradebook

Write these functions (API), then add a loop to allow user to select which to call

5 of 10

saving state in files

JSON - Javascript Object Notation

at this point, basically nothing to do with javascript

just a file format for saving dictionaries

Also common: YAML

JSON: often used for data (but not too much data)�YAML: a bit more human readable, often used for configuration files

6 of 10

A quick note on databases

NoSQL vs MySQL

e.g. MongoDB

7 of 10

JSON basics in python

import json

sample = {"test": 36}�

with open('data.json', 'w') as json_file:

json.dump(sample, json_file)

with open('data.json') as json_file:

data = json.load(json_file)

print(data["test"])

8 of 10

reading and writing other files

9 of 10

Interactive gradebook

Allow user to:

add_student(gradebook): adds a student with no grades to the gradebook, if student already exists, do nothing. return the updated gradebook.

add_grade(gradebook, student, assignment, grade): add a grade to that student, return the updated gradebook

save_gradebook(gradebook, fp): writes the gradebook to a json file, no return

load_gradebook(fp): returns loaded gradebook

Write these functions (API), then add a loop to allow user to select which to call

10 of 10

Intro to HW3

to be posted when get here

https://replit.com/@MarkSantolucito/IEE1164Homework3

also, what to do tomorrow?