0

I have two classes Course and Student. Course has a function that grades a student and gives a score. I'm trying to build a function in Student that takes that grade and shows the total score for a particular student.

class courseClass(object):

def grade(self, student, grade):
        self.grade = grade
        if self.grade == 1:
            print("Student passed mandatory assignment")
        elif self.grade == 0:
            print("Student failed mandatory assignment")
        elif self.grade != 0 or 1:
            raise Exception("score is out of pattern range")

course_instance = courseClass()
course_instance.grade(student1, 1) 


class Student(object):

def grade_status(self, student):
        return [i.grade for i in self.grade]

student1 = Student("Bob Bobson", 20, 58008)
x = Student.grade_status(student1)
print(x)

AttributeError: 'Student' object has no attribute 'grade'

I think it needs to something like this instead:

def grade_status(self, grade):
        self.grade =

But I don't know how to make it equal to the grade that's being given at the grade function (and will it know which student it is assigned to?)

2
  • You haven't modeled this carefully as to what the relationship between a Course and a Student is and their responsibilities. One possibility: a Student instance has-a set of Course instances, each of which has a grade. What would the methods be assigning a Course instance to a Student instance and assigning a grade to that Course instance? If the school teaches history and geometry, would you model these as separate classes? A course needs to know who is enrolled and needs to keep a set of students. A student needs a set courses he is enrolled in. Are these "courses" the same Commented Sep 23, 2020 at 13:16
  • @Booboo How do assign a Student instance a Course Instance? That's exactly the problem I want to solve. Commented Sep 23, 2020 at 14:10

2 Answers 2

1

Here is just one design (feel free to change the grading system):

class Course:
    """
    Models a course being taught, for example: geometry
    """

    def __init__(self, course_name):
        self._course_name = course_name
        self._enrollment = {} # No students so far


    @property
    def course_name(self):
        """Get the course_name."""
        return self._course_name


    def enroll_student(self, student):
        """
        enroll student in this course
        """
        self._enrollment[student] = None # no grade so far
        student.add_course(self) # show that student is taking this course
        return self


    def enrollment(self):
        """
        generate students enrolled in course
        """
        for student, grade in self._enrollment.items():
            yield student, grade


    def assign_grade(self, student, grade):
        """
        assign grade to a student
        """
        assert student in self._enrollment
        self._enrollment[student] = grade
        return self


    def get_grade(self, student):
        """
        return a student's grade
        """
        return self._enrollment[student]


class Student:
    """
    Models a student
    """

    def __init__(self, name):
        self._name = name
        self._courses = []


    @property
    def name(self):
        """Get student name"""
        return self._name


    def add_course(self, course):
        self._courses.append(course)
        return self


    def courses(self):
        for course in self._courses:
            yield course


geometry = Course('geometry')
history = Course('history')
john_doe = Student('john doe')
jane_doe = Student('jane_doe')

geometry.enroll_student(john_doe)
geometry.enroll_student(jane_doe)
history.enroll_student(jane_doe)

geometry.assign_grade(john_doe, 1)
geometry.assign_grade(jane_doe, 2)
history.assign_grade(jane_doe, 1)

# print all the geometry grades
for student, grade in geometry.enrollment():
    print(student.name, grade)

# print all of john_doe's courses and grades:
for course in john_doe.courses():
    print('john_doe:', course.course_name, course.get_grade(john_doe))
for course in jane_doe.courses():
    print('jane_doe:', course.course_name, course.get_grade(jane_doe))

Prints:

john doe 1
jane_doe 2
john_doe: geometry 1
jane_doe: geometry 2
jane_doe: history 1
Sign up to request clarification or add additional context in comments.

Comments

0

I dont know if this what are trying to do

class courseClass(object):

def __init__(self, student, grade):
    self.student = student
    self.grade = grade

def grade_status(self):
    if self.grade == 1:
        return "Student passed mandatory assignment"
    elif self.grade == 0:
        return "Student failed mandatory assignment"
    elif self.grade != 0 or 1:
        raise Exception("score is out of pattern range")




class Student(object):
    def __init__(self, name, age, random_number):
        self.name = name
        self.age = age
        self.random_number = random_number

student1 = Student("Bob Bobson", 20, 58008)
course_instance = courseClass(student1,0)
x = course_instance.grade_status()
print(x)

1 Comment

Why include a random number in the init though? I didn't include my own in the post to save mental overhead but it takes and ID instead og random number. But what i want is to call a function in Student that pulls the grade given in Course

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.