AttributeError: у объекта 'str' нет атрибута 'courseGrade' - PullRequest
1 голос
/ 13 мая 2019

Я получаю ошибку AttributeError: у объекта 'str' нет атрибута 'courseGrade', и я не знаю, как продолжить. Я не уверен, как я создал ошибку атрибута. Тем не менее, я не часто использую Python.

class course:
    def __init__(self, courseName, courseNumber, courseGrade):
        self.courseName = courseName
        self.courseNumber = courseNumber
        self.courseGrade = courseGrade




#%%     

# student class. Tracks student name and cumulative GPA    
# default GPA is 0 unless passed in for a transfer student
# courses will be a list of courses the student takes


class student:
    def __init__(self,firstName,lastName, cumulativeGPA = 0):
        self.firstName = firstName
        self.lastName = lastName
        self.cumulativeGPA = cumulativeGPA
        self.courses = []

# method to add courses to the student transript
    def enterGrade(self, courseNumber, courseName, courseGrade):
        newCourse = (courseName, courseNumber, courseGrade)
        self.courses = newCourse
        self.cumulativeGPA = self._updateGPA(courseGrade)

# private method to maintain the student GPA        
    def _updateGPA(self,newGrade):
        totalGPA = 0
        for x in self.courses:
            totalGPA += totalGPA + x.courseGrade
            numCourses += 1
        self.cumulativeGPA += round(totalGPA//numCourses,2)

# method to print the student transcript.        
    def printTranscript(self):
        print(self.lastName, ' ', self.firstName, ' GPA 
',self.cumulativeGPA)
        for x in self.courses:
            print('    ', x.courseNumber, '  ', x.courseName, '  ', 
x.courseGrade)

1 Ответ

2 голосов
/ 13 мая 2019

Эта строка в вашем enterGrade методе:

self.courses = newCourse

Изменит атрибут courses с list на course объект, я думаю, что вы хотели сделать, это добавить курс к list, что вы можете сделать, используя метод list.append(). Замените упомянутую строку на эту:

self.courses.append(newCourse)
...