suppose we have a dictionary called grades as shown below:

grades = {“John”: [87,90,86.8], “Mary”: [78, 81.5, 88.6], …}

The keys are names of students (assume that the names are unique) and the value associated with each is a list of three exam scores. Write a program that prints a report card in the following format:

John 87.93 B

Mary 82.70 B

Assume that an average of 90 and above is an “A”, 80 to 89.999 is a “B”, 70 to 79.9999 is a “C” and so on. Anything below 60 is an “F”

Respuesta :

Answer:

import statistics as st # importing statistics package to calculate mean as st.

grades = {"John": [87,90,86.8], "Mary": [78, 81.5, 88.6],"Susan":[45,67,76]}

for key,value in grades.items():#iterating over the dictionary

   average_score=st.mean(value)#calculating the mean of every student

   gr="" empty string to store the grades

   if average_score>=90: # nested if else if and statements for the grades in gr

       gr="A"

   elif average_score>=80.0 and average_score<90.0:

       gr="B"

   elif average_score>=70.0 and average_score<80.0:

       gr="C"

   elif average_score>=60.0 and average_score<70.0:

       gr="D"

   else :

       gr="F"

       

   print(str(key)+" "+str(average_score)+" "+str(gr)) #printing grades of every student.

Output:-Mary 82.7 B

John 87.93333333333334 B

Susan 62.666666666666664 D

Explanation:

1.In this program we are iterating over the dictionary grades.

2.Storing and calculating the mean of the scores of every student us statistics package.

3.Storing the grade in string gr and updating the gr according to the mean score

4.After that printing the grade of the student with their name and mean score.