202k views
0 votes
Question 7

Consider the following scenario about using Python dictionaries:

A teacher is using a dictionary to store student grades. The grades are stored as a point value out of 100. Currently, the teacher has a dictionary setup for Term 1 grades and wants to duplicate it for Term 2. The student name keys in the dictionary should stay the same, but the grade values should be reset to 0.

Complete the “setup_gradebook” function so that input like “{"James": 93, "Felicity": 98, "Barakaa": 80}” will produce a resulting dictionary that contains “{"James": 0, "Felicity": 0, "Barakaa": 0}”. This function should:

accept a dictionary “old_gradebook” variable through the function’s parameters;

make a copy of the “old_gradebook” dictionary;

iterate over each key and value pair in the new dictionary;

replace the value for each key with the number 0;

return the new dictionary.

2 Answers

3 votes

Final answer:

To duplicate a gradebook for Term 2, the setup_gradebook function should make a copy of the old_gradebook dictionary, iterate over each key-value pair, and reset the grades to 0.

Step-by-step explanation:

The given scenario involves a teacher using a Python dictionary to store student grades, with the grades represented as point values out of 100. The task is to create a function called 'setup_gradebook' that will create a duplicate gradebook for Term 2, where the student names remain the same, but the grades are reset to 0. To achieve this, the function should take an 'old_gradebook' dictionary as input, make a copy of it, iterate over each key-value pair in the new dictionary, and replace the value for each key with 0.

User Aviss
by
8.4k points
4 votes

Final answer:

The setup_gradebook function is defined to accept a dictionary of student grades, create a copy of that dictionary, reset each student's grade to 0, and return the new dictionary with the updated grades.

Step-by-step explanation:

To complete the setup_gradebook function in Python, we need to make a copy of the old_gradebook dictionary and reset the values to 0. Below is a step-by-step explanation of how this can be achieved:

  • Define the function setup_gradebook and have it accept one parameter, old_gradebook.
  • Create a new dictionary by copying the old_gradebook.
  • Iterate over the new dictionary's items and set each value to 0.
  • Return the new dictionary with the reset values.

Here is a sample implementation of the setup_gradebook function:
def setup_gradebook(old_gradebook):
new_gradebook = old_gradebook.copy()
for student in new_gradebook:
new_gradebook[student] = 0
return new_gradebook
When you use this function and provide a dictionary like {"James": 93, "Felicity": 98, "Barakaa": 80}, the result will be a new dictionary with zeroes for all grades: {"James": 0, "Felicity": 0, "Barakaa": 0}.

User Nagarjun Prasad
by
8.1k points