2014-07-22 3 views
-2

오늘 세 번째 질문입니다. 그러나 이것은 아주 새로운 프로그램입니다. 그래서 지금은이 오류를 받고 있어요 (당신이 내 코드 오류 투성이라고하기 전에, 나는 기대하고있다.) :Python-AttributeError : 'function'객체에 'course_code'속성이 없습니다.

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() 

모든 도움을 주실 수 있습니다! 고맙습니다!

+2

예, 인스턴스 메소드이며 속성이 없습니다. 너 정확히 뭘 하려구? 왜'courses_done'은리스트를 담고있는 단일 엘리먼트 튜플인가? 왜 어떤 초기화 작업은'__str__'에서 끝났습니까? – jonrsharpe

+0

'College_Student.complete_class'는 함수입니다. 'College_Student.complete_class.course_code'는 함수에 대한 유효하지 않은 조회입니다. –

+0

그 라인으로 무엇을하려합니까? –

답변

0

나는 당신이 뭘 하려는지 이해한다면 - 변경 :

College_Student.complete_class.course_code += self.courses_done 

참고

self.courses.done.append(course_code) - 

에 :

self.courses.done += self.course_code 

도 있지만 작동 @jonsharpe에 의해 지적되어 덜 명시 적. 또한

: complete_class에서

  1. 는 [-]로 self.courses_done을 설정하지 마십시오 - 할 초기화에 있음 - 곳이 순간에있다가 코스 목록을 학생들에게 각각의 시간을 재설정합니다 그들이 과정을 완료하십시오
  2. str 메서드에서 인스턴스 변수를 변경하는 것은 나쁜 형식입니다.이 메서드는 해당 학생의 텍스트 표현을 제공하는 데 사용되어야합니다. 코드를 변경 했으므로 분명한 이유는 없다.
+1

그것은'courses_done'이어야하고 당신은 목록에'append'합니다. – jonrsharpe

+0

이것이 작동하는 것처럼 보입니다 ... 죄송합니다. 실제로 문제를 설명하지 않았습니다. –

+0

@jonsharpe - (명시 적으로) append를 사용해야한다는 것에 동의하지만 "+ ="또한 목록에 적용됩니다. 추가 답변을 사용하기 위해 내 대답을 수정했습니다. –