Problem 4.5.4, Part 1 of 4 0.0/1.0 point (graded) In the last problem, we create
ID: 3709354 • Letter: P
Question
Problem 4.5.4, Part 1 of 4 0.0/1.0 point (graded) In the last problem, we created a basic gradebook dictionary that paired student names (the keys) with grades (the values) In a real gradebook, though, an individual's average is broken into multiple pieces: perhaps a homework grade, a test grade, and an exam grade Let's create our gradebook such that instead of a dictionary of name-grade pairs, it is instead a list of dictionaries, and each dictionary has four keys: the string name, whose value is the name of the student as a string: the string homework, whose value is the student's homework grade as an integer; the string test, whose value is the student's test grade as an integer; and the string exam, whose value is the student's exam grade as an integer. For example, this little block of code would create a new dictionary for the student "Samantha", and add it to the gradebook: newstudent -("name": "Samantha", "homework: 95, test gradebook.append (newStudent) 92, "exam: 100) So, we have a list called gradebook that contains a list of dictionaries, where each dictionary contains four keys: name, homework, test, and exam. Imagine you wanted to get the entire dictionary for the first student in this list. What line of code would retrieve that dictionary?Explanation / Answer
gradebook = []
newStudent = {'name': 'Samantha', 'homework':95, 'test':92, 'exam':98}
gradebook.append(newStudent)
# let's add few more students
newStudent = {'name': 'Robin', 'homework':87, 'test':97, 'exam':95}
gradebook.append(newStudent)
newStudent = {'name': 'Don', 'homework':92, 'test':99, 'exam':99}
gradebook.append(newStudent)
newStudent = {'name': 'Andy', 'homework':95, 'test':92, 'exam':98}
gradebook.append(newStudent)
# 1. To get the entire dictionary for the first student
print gradebook[0]
# 2. To access the first student's name only
print gradebook[0]['name']
# 3. To change the first student's exam marks to 100
gradebook[0]['exam'] = 100
# 4. To convert the old gradebook into newGradebook
newGradebook = {student_info['name'] : student_info for student_info in gradebook}
print newGradebook # to print the newly created dictionary
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.