average in a dictionary scores-python-tutorial-Python-courses-python-exercises

Exercise 118

The following dictionary contains a student's math scores:

scores = {'score1' : 16, 'score2' : 14, 'score3' : 17}

update the notes dictionary by adding the average of scores.

Solution

scores = {'score1' : 16, 'score2' : 14, 'score3' : 17}

# initializing the average of scores
average = 0
# browsing through all items in dictionary
for key , value in scores.items():
average = average + scores[key]
average = average/3
scores['avaerage'] = average

# Testing program
print("The updated dictionary is : scores = " , scores)
# display : The updated dictionary is : scores = {'score1': 16, 'score2': 14, 'score3': 17, 'avaerage': 15.666666666666666}

Younes Derfoufi
my-courses.net

Leave a Reply