Python- 属性错误:'function'对象没有'course_code'属性

-2 投票
1 回答
9088 浏览
提问于 2025-04-18 14:21

今天的第三个问题。不过这是一个全新的程序。所以现在我遇到了这个错误(在你说我的代码满是错误之前,我已经做好心理准备了):

Traceback (most recent call last):
  File "C:/Users/DDeahr/Downloads/College_Student.py", line 58, in <module>
    main()
  File "C:/Users/DDeahr/Downloads/College_Student.py", line 46, in main
    Alex.complete_class("English 101", 10)
  File "C:/Users/DDeahr/Downloads/College_Student.py", line 30, in complete_class
    College_Student.complete_class.course_code += self.courses_done
AttributeError: 'function' object has no attribute 'course_code'

新的错误:

Traceback (most recent call last):
  File "C:/Users/DDeahr/Downloads/College_Student.py", line 1, in <module>
    class College_Student(object):
  File "C:/Users/DDeahr/Downloads/College_Student.py", line 30, in College_Student
    course_code = staticmethod(course_code)
NameError: name 'course_code' is not defined

这是我的代码:

class College_Student(object):
    total = 0
    enrolled = []

    def __init__(self, first_name, last_name, id_num, courses_done, credit_hrs):
        self = self
        self.first_name = first_name
        self.last_name = last_name
        self.id_num = id_num
        self.courses_done = [],
        self.credit_hrs = credit_hrs

    def __str__(self):
        first_name = self.first_name
        last_name = self.last_name
        id_num = self.id_num
        courses_done = self.courses_done
        credit_hrs = self.credit_hrs
        College_Student.total += 1
        College_Student.enrolled += self.last_name
        College_Student.enrolled.sort()
        return "First Name: %s\nLast Name: %s\nID Number: %s\nCourses Finished: %s\nCredit Hours: %s\n" % (self.first_name, self.last_name, self.id_num, self.courses_done, self.credit_hrs)

    def complete_class(self,course_code,student_credit_hrs):
        self.credit_hrs += student_credit_hrs
        self.courses_done = []
        College_Student.complete_class.course_code += self.courses_done
        return "Student with ID number: %s has finished course %s which is %s credit hours." % (self.id_num, self.course_code, self.student_credit_hours)

    def can_grad(self):
        if self.credit_hrs >= 120:
            return "Student with ID number: %s can now graduate." % (self.id_num)
        else:
            return "Student with ID number: %s cannot graduate yet." % (self.id_num)

def main():
    print "Creating student John"
    John = College_Student("John", "Appleseed", 111111, None, 20)
    print John
    print "Creating student Alex"
    Alex = College_Student("Alex", "Trannon", 222222, None, 30)
    print Alex
    Alex.complete_class("English 101", 10)
    Alex.complete_class("Mathmatics 102", 20)
    Alex.complete_class("Computer Sciences 208", 60)
    John.complete_class("Psychology 5005", 40)
    John.complete_class("English 108.365", 2)
    John.complete_class("Chinese 101", 10)
    John.complete_class("Computer Sciences 30", 28)
    Alex.can_grad()
    John.can_grad()
    print total
    print enrolled

main()

任何帮助都非常感谢!谢谢你!

1 个回答

0

如果我理解你想做的事情没错的话,就是把:

College_Student.complete_class.course_code += self.courses_done

改成

self.courses.done.append(course_code) - 

注意:

self.courses.done += self.course_code

这种方法也可以,但正如@jonsharpe指出的那样,它不够明确。

还有:

  1. 在complete_class里,不要把self.courses_done设置为[],应该在init里设置。因为现在这样做的话,每次学生完成一门课程时,都会重置他们的课程列表。
  2. str方法中改变实例变量是不好的做法。这个方法应该只是用来提供学生的文本表示,而不是像你现在这样,似乎在做一些没有明显理由的修改。

撰写回答