Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Code not running: "TypeError: 'list' object is not callable"

Hi my code is not working i have checked the related questions but did not come up with a solution.

game.py:

  .fun {
    background-color: yellow;
    color: pink;
    font-size: 5000px;
  }
from cards import Card
import random

class Game:
    def __init__(self):
        self.size = 4
        self.card_options = ['Add', 'Boo', 'Egg', 'Hut', 'Tab', 'Nut', 'Woo', 'Top']
        self.columns = ['A', 'B', 'C', 'D']
        self.cards = []
        self.locations = []
        for column in self.columns:
            for num in range(1, self.size + 1):
                 self.locations.append(f'{column}{num}')

    def set_cards(self):
        used_locations = []
        for name in self.card_options:
            for i in range(2):
                available_locations = set(self.locations) - set(used_locations)
                random_location = random.choice(list(available_locations))
                used_locations.append(random_location)
                card = Card(name, random_location)
                self.cards.append(card)



#methods
    #create cards
    #create grid
    #check for matches
    #check game won
    #run the game

#dunder main
if __name__ == '__main__':
    game = Game()
    game.set_cards()
    for card in game.cards():
        print(card)

    #create game instance
    #call start game 

cards.py:

  .fun {
    background-color: yellow;
    color: pink;
    font-size: 5000px;
  }

class Card():
    def __init__(self, name, location):
        self.name = name
        self.location = location
        self.matched = False

    def __eq__(self, other):
        return self.name == other.name

    def __str__(self):
        return f'Your cards is {self.name}'

if __name__ == '__main__':
    card1 = Card('egg', 'A1')
    card2 = Card('hut', 'B4')
    card3 = Card('egg', 'C3')

    print(card1 == card2)
    print(card1 == card3)
    print(card3 == card2)
    print(card1)

The error code I receive is: Traceback (most recent call last):
File "/home/treehouse/workspace/game.py", line 38, in <module>
for item in game.cards():
TypeError: 'list' object is not callable

Thank you in advance

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Hey Frederic Stein, in the dunder main code for loop, you loop over game.cards()

    for card in game.cards():
        print(card)

game is an instance of Game. The cards attribute is defined by the __init__ code self.cards = [], then this list is further defined in set_cards method using the line self.cards.append(card).

By placing a () at the end of game.cards() you are calling the function game.cards. Remove the parens so that you are looping over the list game.cards instead of trying to loop over the results of the call to the non-existent game.cards() function.

Post back if you need more help. Good luck!!

Thank you very much for your help!