简体   繁体   中英

Python Class Setter Method Works Only Once

class courseInfo(object):

    def __init__(self, courseName):
        self.courseName = courseName
        self.grade = "No Grade"


    def setGrade(self, grade):
        if self.grade == "No Grade":
            self.grade = grade

    def getGrade(self):
        return self.grade



class edx(object):
    def __init__(self, courses):
        self.myCourses = []
        for course in courses:
            self.myCourses.append(courseInfo(course))


    def setGrade(self, grade, course="6.01x"):
        """
        grade:integer greater than or equal to 0 and less than or equal to 100
        course: string 

        This method sets the grade in the courseInfo object named by `course`.   

        If `course` was not part of the initialization, then no grade is set, and no
        error is thrown.

        The method does not return a value.
        """
        for crs in self.myCourses:
            if crs.courseName == course:
                crs.setGrade(grade)



    def getGrade(self, course="6.02x"):
        """
        course: string 

        This method gets the grade in the the courseInfo object named by `course`.

        returns: the integer grade for `course`.  
        If `course` was not part of the initialization, returns -1.
        """
        for crs in self.myCourses:
            if crs.courseName == course:
                return crs.getGrade()
            else:
                return -1            

My test cases for the code above are:

> edX = edx( ["6.00x","6.01x","6.02x"] )
> edX.setGrade(100, "6.00x")
> edX.getGrade("6.00x")
> 100

So far so good, but running edX.setGrade(50, "6.00x") doesn't take and getGrade still returns 100 . Also, setting grades for 6.01 and 6.02 doesn't seem to work and getGrade returns -1 all the time.

Any help pointers would be much appreciated!

(Full disclosure: this is for an on-line course. But I don't think there are spoilers here for anyone and I really want to understand what's going on. Tks)

Of course it only works once, you coded it that way:

def setGrade(self, grade):
    if self.grade == "No Grade":
        self.grade = grade

After setting the grade, the test self.grade == "No Grade" is no longer true.

You do have a problem in the getGrade() method:

for crs in self.myCourses:
    if crs.courseName == course:
        return crs.getGrade()
    else:
        return -1            

You return -1 if the first course doesn't match the name; you return from the for loop right there. Perhaps you want to return -1 only after testing all courses :

for crs in self.myCourses:
    if crs.courseName == course:
        return crs.getGrade()
return -1            

Now the for loop is only interrupted if the matching course is found, and you no longer return -1 for anything other than "6.00x" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM