1

I am new to python and struggling with a homework problem. I need to create a method to count the number of credits a student has accumulated, and return that number as a float

I am at a loss on how to proceed. I have listed the two classes I have created, and need

class Course(object):


    def __init__(self, title, year, semester, credits, grade):
        self.title = title
        self.year = year
        self.semester = semester
        self.credits = credits
        self.grade = grade

class StudentRecord(object):


    def __init__(self, firstName, lastName, dob, hsName, hsCity, hsState, degree, address, transcript = [], newcourse = []):
        self.firstName = firstName
        self.lastName = lastName
        self.dob = dob
        self.hsName = hsName
        self.hsCity = hsCity
        self.hsState = hsState
        self.degree = degree
        self.address = address
        self.transcript = transcript
        self.newcourse = newcourse



    def credits():

I know the first two classes work in my Mimir assignment but am struggling on how to proceed with the def credits():

Any help would be greatly appreciated.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
Seth N
  • 11
  • 1
  • 1
    What is `transcript`? What is `newcourse`? How are those related to the number of credits the student has earned? – Patrick Haugh Feb 03 '19 at 00:00
  • The homework question utilizes those variables later on. I was hoping once I figure out how to proceed with this first part that I could work through the remainder of the problem. – Seth N Feb 03 '19 at 00:06
  • For me it seems like a student should have a list of courses, right? I can't see anything like that there – user8408080 Feb 03 '19 at 00:09
  • My understanding is that we write the code, and then the professor will enter all the data to test to see if the code works. He doesn't show us what the courses or grades are. He only provided the variables listed in the two classes I made. – Seth N Feb 03 '19 at 00:20

1 Answers1

1

It looks like transcript will be the list of Courses the student has completed, while newcourse is the list of Courses being completed this semester. If so, you need to loop over transcript summing course.credits:

def credits(self):
    return sum(course.credits for course in self.transcript)

You should also read this question and its answers, to learn why the default arguments in your __init__ may cause problems.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96