TypeError:__str__返回非字符串(类型NoneType)

时间:2018-04-02 18:23:48

标签: python inheritance nonetype

因此,对于我在uni的python课程中的最后一次分配,我必须编写一个由三个对象组成的程序,其中两个继承。我一直陷入困境,特别是关于最后两个对象。这是我的代码:

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)

在第60行附近我收到错误:
TypeError: str 返回非字符串(类型NoneType) 我很遗憾发生了什么事。

2 个答案:

答案 0 :(得分:3)

您的__repr__未明确返回任何内容。您构建一个字符串,然后将其丢弃,导致None被隐式返回。

只需添加return

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

答案 1 :(得分:0)

调整原始问题的源代码:

  • def dropStudent(self,stu):
  • 添加遗失语句
  • return
  • 添加缺失的def __repr__(self):表达式
  • StudentCourse(Course) init的签名调整为def __init__(self,title,ID,grade):以与父类一致并按预期处理给定语句StudentCourse("History of Nu-Metal",57859,82)
  • 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()