Implement a class Student
that stores grades for different subjects. The class should have methods to calculate the average grade, highest grade, and lowest grade.
Example 1:
Input: student = Student() student.add_grade("Math", 90) student.add_grade("English", 85) student.add_grade("Science", 92) print(student.calculate_average()) print(student.find_highest()) print(student.find_lowest()) Output: 89.0 92 85
Example 2:
Input: student = Student() student.add_grade("History", 88) student.add_grade("Geography", 91) print(student.calculate_average()) print(student.find_highest()) print(student.find_lowest()) Output: 89.5 91 88
Use a dictionary to store the subjects and their corresponding grades. The dictionary keys can be the subjects and the dictionary values can be the grades.
class Student: def __init__(self): self.grades = {} def add_grade(self, subject, grade): self.grades[subject] = grade def calculate_average(self): return sum(self.grades.values()) / len(self.grades) def find_highest(self): return max(self.grades.values()) def find_lowest(self): return min(self.grades.values())
Unlock AI & Data Science treasures. Log in!