Home > Net >  I'm new to coding and I can't find the problem here, line 36 AttributeError: 'tuple&#
I'm new to coding and I can't find the problem here, line 36 AttributeError: 'tuple&#

Time:08-10

class ClubMembers:

    def __init__(self, name, birthday, age, favorite_food, goal):
        self.name = name
        self.birthday = birthday
        self.age = age
        self.favorite_food = favorite_food
        self.goal = goal

    def display1(self):
        print('Name: ', self.name)
        print('Birthday: ', self.birthday)
        print('Age: ', self.age)
        print('favorite Food: ', self.favorite_food)
        print('Goal: ', self.goal)


class ClubOfficers(ClubMembers):

    def __init__(self, name, birthday, age, favorite_food, goal, position):
        self.position = position
        ClubMembers.__init__(self, name, birthday, age, favorite_food, goal)

    def display2(self):
        print('Name: ', self.name)
        print('Birthday: ', self.birthday)
        print('Age: ', self.age)
        print('favorite Food: ', self.favorite_food)
        print('Goal: ', self.goal)
        print('Position: ', self.position)


cm_1 = ('Tom', 'January 16', '14', 'Mami', 'To be happy')

o_1 = ('Vera', 'June 22', '16', 'Bulalo', 'Mapasakin ka <33', 'President')

cm_1.display1()

o_1.display2()

CodePudding user response:

Rather than instantiating your custom classes with arguments in the parentheses, you're just making a plain tuple object. Add the name of your class that you want an instance of in front of the parentheses.

These

cm_1 = ('Tom', 'January 16', '14', 'Mami', 'To be happy')
o_1 = ('Vera', 'June 22', '16', 'Bulalo', 'Mapasakin ka <33', 'President')

Become this

cm_1 = ClubMembers('Tom', 'January 16', '14', 'Mami', 'To be happy')
o_1 = ClubOfficers('Vera', 'June 22', '16', 'Bulalo', 'Mapasakin ka <33', 'President')

CodePudding user response:

You don't instantiate your class at all, instead you are making a tuple.

To make it work, you should call class constructor:

See the difference:

some_tuple  = ('Tom', 'January 16', '14', 'Mami', 'To be happy')  # it's a tuple

print(some_tuple[1])  # It will print January 16
some_tuple.name  # ERROR: Tuple is just a sequence of values, it doesn't have properties
some_tuple.display1()  # ERROR: There is no display1 method for tuple

some_object = ClubMembers('Tom', 'January 16', '14', 'Mami', 'To be happy')  # it's object of type ClubMembers
print(some_object.name)  # OK!
some_object.display1()  # OK!
  • Related