Student Gradebook Management

Write a class StudentGradebook with private attributes for grades in different subjects. Include methods to add and retrieve grades, ensuring that grades are within valid limits.

Example 1:

Input: Add Grade: "Math", 90
Output: "Grade added"

Example 2:

Input: Add Grade: "Science", 110
Output: "Invalid grade"

Maintain a dictionary for subjects and their grades. Check the grade before adding.

class StudentGradebook:
    def __init__(self):
        self._grades = {}

    def add_grade(self, subject, grade):
        if 0 <= grade <= 100:
            self._grades[subject] = grade
            return "Grade added"
            return "Invalid grade"

    def get_grade(self, subject):
        return self._grades.get(subject, "No grade for this subject")

# Test the class
gradebook = StudentGradebook()
gradebook.add_grade('Math', 90)  # Output: Grade added
gradebook.add_grade('Science', 110)  # Output: Invalid grade

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!