简体   繁体   中英

AttributeError: 'str' object has no attribute 'member' Error

I am trying to make a GPA calculator for my coding class. I keep running into this error and I hope you can fix it.

class Student:
    role = 'learn'
    
    def __init__(self,name,grade,fav_color,school,GPA):
        self.name = name
        self.grade = grade
        self.fav_color = fav_color
        self.school = school
        self.GPA = GPA

class School:
    roster = []
    
    def __init__(self, school_name):
        self.school_name = school_name
    
    def add_members(self):
        name = input('What is their name')
        is_student = input('Are they a student? (y/n) ')
        if is_student == 'y':
            grade = input('What grade are they in?')
            school = input('What school do they go to?')
            mathGPA = int(input('How much GPA does the student have for math? '))
            sciGPA = int(input('How much GPA does the student have for science? '))
            socialGPA = int(input('How much GPA does the student have for social? '))
            laGPA = int(input('How much GPA does the student have for LA? '))
            
            addedGPA = int(mathGPA + sciGPA + socialGPA + laGPA)
            
            print(addedGPA)
            
            calculatedGPA = (addedGPA / 4)
            new_member = Student(name, grade, 'red', school, calculatedGPA)
        else:
            subject = input('What do they teach? ')
            school = input('What school do they go to?')
            new_member = Teacher(name, subject, school)
        
        self.roster.append(new_member)
        
        if input('Add another member (y/n) ') == 'y':
            self.add_members()
    
    def show_roster(self):
        for member in self.roster:
            if isinstance(member, Teacher):
                print('{} is a teacher, they teach {} at {}'.format(member.name, member.subject, member.school))
            elif isinstance(member, Student):
                print('{} is a student, they are in grade {} at this school called {}. Their GPA is {}'.format(member.name, member.grade, member.school. member.GPA))

The error:

Traceback (most recent call last):
  File "C:\Users\kp876\Desktop\Python\Python projects\1 26 2021 lesson 3.py", line 64, in <module>
    my_school.show_roster()
  File "C:\Users\kp876\Desktop\Python\Python projects\1 26 2021 lesson 3.py", line 57, in show_roster
    print('{} is a student, they are in grade {} at this school called {}. Their GPA is {}'.format(member.name, member.grade, member.school. member.GPA))
AttributeError: 'str' object has no attribute 'member'

I am trying to make it say what their average GPA is but the error keeps on getting in my way. I don't know how to fix it and I hope you can!

There was a typo in print('{} is a student, they are in grade {} at this school called {}. Their GPA is {}'.format(member.name, member.grade, member.school. member.GPA))

I did a. instead of a, infront of the member.GPA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM