我的代码不断出现错误“AttributeError:“Student”对象没有“on_honor_roll”属性”我应该怎么做才能修复它?

2024-09-29 03:40:56 发布

您现在位置:Python中文网/ 问答频道 /正文

from Student import Student

student1 = Student("Oscar", "Accounting", 3.1)
student2 = Student("Phyllis", "Business", 3.8)

print(student1.on_honor_roll())

class Student:
    def __init__(self, name,  major, gpa):
        self.name = name
        self.major = major
        self.gpa = gpa

        def on_honor_roll(self):
            if self.gpa >= 3.5:
                return True
            else:
                return False

这是我的代码,下面是我关注的视频链接。请尝试帮助我,因为这是如此令人沮丧,因为我被困在这几个星期:(!!!!!!!)


Tags: namefromimportselfreturnondefstudent
1条回答
网友
1楼 · 发布于 2024-09-29 03:40:56
class Student: 
    def __init__(self, name, major, gpa): 
        self.name = name 
        self.major = major 
        self.gpa = gpa

    def on_honor_roll(self): # Correct the Indent Here
        if self.gpa >= 3.5:
            return True
        else:
            return False

student1 = Student("Oscar", "Accounting", 3.1) 
student2 = Student("Phyllis", "Business", 3.8)

print(student1.on_honor_roll(), student2.on_honor_roll())

输出:

False True

您所犯的错误: def on_honor_roll(self):应该直接到def __init__(self, name, major, gpa):我的意思是它们应该具有相同的缩进级别

相关问题 更多 >