简体   繁体   中英

TypeError: __str__ returned non-string (type NoneType)

So for my last assingment in my python course at uni, I have to write a program consisting of three objects, two of which inherit. I keep running into a snag especially with regards to the last two objects. Here is my code:

class Course:
    def __init__(self,title="",ID=0):
        self._ID = ID
        self._title = title

    def getID(self):
        return self._ID

    def getTitle(self):
        return self._title

    def setTitle(self,title):
        self._title = title

    def setID(self,ID):
        self._ID = ID

    def __repr__(self):
        return "Title: " + self._title + "ID: " + str(self._ID)

class OfferedCourse(Course):
    def __init__(self,title="",ID=0,enrollment=[]):
        super().__init__(title,ID)
        self._enrollment = len(enrollment)

    def getEnrollment(self):
        return self._enrollment

    def addStudent(self,stu):
        if stu in enrollment:
            print("Student is already enrolled.")
        else:
            enrollment.append(stu)

    def dropStudent(self,stu):
        if stu in enrollment:

    def __repr__(self):
        super().__repr__() + "Enrollment: " + str(self._enrollment)

class StudentCourse(Course):
    def __init__(self,grade,ID=0,title=""):
        super().__init__(title,ID)
        self._grade = grade

    def getGrade(self):
        return self._grade

    def setGrade(self,grade):
        self._grade = grade

    def __repr__(self):
        super().__repr__() + "Grade: " + str(self._grade)   
def main():
#Set primary course
lego=Course("Lego Design",32013)
#display course
print(lego)
#Set OfferedCourse
bonk=OfferedCourse("Matoran History",82932,["Josh","Rick","Greg","Chris"])
#Display OfferedCourse
print(bonk)
#Set StudentCourse
lp=StudentCourse("History of Nu-Metal",57859,82)
#display Student Course
print(lp)

At around line 60 I recieve the error:
TypeError: str returned non-string (type NoneType) I'm pretty lost as to what is going on.

Your __repr__ s don't explicitly return anything. You build up a string, then throw it away, causing None to be implicitly returned instead.

Just add a return :

def __repr__(self):
  return super().__repr__() + "Grade: " + str(self._grade) 

Adjustments to the source code of the original question:

  • add missing statement at def dropStudent(self,stu):
  • add missing return expression for def __repr__(self):
  • adjust signature of StudentCourse(Course) init to def __init__(self,title,ID,grade): to be in line with parent classes and process given statement StudentCourse("History of Nu-Metal",57859,82) as expected
  • add missing indentions for def main():

class Course:
    def __init__(self,title="",ID=0):
        self._ID = ID
        self._title = title

    def getID(self):
        return self._ID

    def getTitle(self):
        return self._title

    def setTitle(self,title):
        self._title = title

    def setID(self,ID):
        self._ID = ID

    def __repr__(self):
        return "Title: " + self._title + "ID: " + str(self._ID)

class OfferedCourse(Course):
    def __init__(self,title="",ID=0,enrollment=[]):
        super().__init__(title,ID)
        self._enrollment = len(enrollment)

    def getEnrollment(self):
        return self._enrollment

    def addStudent(self,stu):
        if stu in enrollment:
            print("Student is already enrolled.")
        else:
            enrollment.append(stu)

    def dropStudent(self,stu):
        if stu in enrollment:
            print("@todo Something is missing here...")

    def __repr__(self):
        return super().__repr__() + "Enrollment: " + str(self._enrollment)

class StudentCourse(Course):
    def __init__(self,title,ID,grade):
        super().__init__(title,ID)
        self._grade = grade

    def getGrade(self):
        return self._grade

    def setGrade(self,grade):
        self._grade = grade

    def __repr__(self):
        return super().__repr__() + "Grade: " + str(self._grade)

def main():
    #Set primary course
    lego=Course("Lego Design",32013)
    #display course
    print(lego)
    #Set OfferedCourse
    bonk=OfferedCourse("Matoran History",82932,["Josh","Rick","Greg","Chris"])
    #Display OfferedCourse
    print(bonk)
    #Set StudentCourse
    lp=StudentCourse("History of Nu-Metal",57859,82)
    #display Student Course
    print(lp)

main()

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