Question:medium

Write a user-defined function in Python named showGrades(S) which takes the dictionary S as an argument. The dictionary S contains Name: [Eng, Math, Science] as key:value pairs. 
The function displays the corresponding grade obtained by the students according to the following grading rules: 

\[ \begin{array}{|c|c|} \hline \textbf{Average of Eng, Math, Science} & \textbf{Grade} \\ \hline \geq 90 & A \\ \hline < 90 \text{ but } \geq 60 & B \\ \hline < 60 & C \\ \hline \end{array} \] 

Example: Consider the following dictionary: \[ S = \{\text{"AMIT"}: [92, 86, 64], \text{"NAGMA"}: [65, 42, 43], \text{"DAVID"}: [92, 90, 88]\} \] The output should be: \[ \text{AMIT} - B \\ \text{NAGMA} - C \\ \text{DAVID} - A \]

Show Hint

When dealing with dictionaries containing lists as values, use .items()} to loop through the key-value pairs and apply logic to the values directly.
Updated On: Jan 13, 2026
Show Solution

Solution and Explanation

def assign_grades(student_data):  # Defines a function to assign grades
    for student_name, scores in student_data.items():  # Iterates through each student and their scores
        average_score = sum(scores) / len(scores)  # Computes the average of the scores
        if average_score>90:  # Checks if the average score qualifies for grade A
            grade = "A"
        elif average_score>60:  # Checks if the average score qualifies for grade B
            grade = "B"
        else:  # Assigns grade C if the average score is below the B threshold
            grade = "C"
        print(f"{student_name} - {grade}")  # Displays the student's name and assigned grade

# Sample student data dictionary
student_records = {"AMIT": [92, 86, 64], "NAGMA": [65, 42, 43], "DAVID": [92, 90, 88]}
assign_grades(student_records)  # Invokes the function with the sample data
    
Explanation: The function assign_grades(student_data) processes a dictionary student_data. Each key in this dictionary represents a student's name, and its associated value is a list of their scores. The average score is computed using the formula sum(scores) / len(scores). Grades are allocated based on the following average score thresholds: - An average score of 90 or higher results in a grade of "A". - An average score between 60 (inclusive) and 90 (exclusive) results in a grade of "B". - An average score below 60 results in a grade of "C". The output is presented in the format Name - Grade.
Was this answer helpful?
0

Top Questions on Commands and Requests