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

TypeError: 'method' object is not iterable

from cards import Card
import random

class Game:
    def __init__(self):
        self.size = 4
        self.card_options = ['Egg', 'Cheese', 'Milk', 'Orange', 'Beans', 'Sugar', 'Celery', 'Ice']
        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}')

    #create new method 
        #keep track of used locations
                #pick one card
                      #pick another card
                        #to remove duplicate use set
                        #pick card at random
                        #pass word and it's location
    def set_cards(self):
        used_locations = []
        for word in self.card_options:
            for i in range(2):
                avaliable_locations = set(self.locations) - set(used_locations)
                random_location = random.choice(list(avaliable_locations))
                used_locations.append(random_location)
                card = Card(word, random_location)
                self.cards.append(card)


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

# method
    #creat card with word
    #creat grid
    #check for matches
    #check if the game have been won
    #run the game

#Dunder main
    #create game instance
    #call the start game

I am getting this when I run the above code, thanks: Traceback (most recent call last):
File "/home/treehouse/workspace/game.py", line 37, in <module>
for card in game.set_cards:
TypeError: 'method' object is not iterable

1 Answer

Steven Parker
Steven Parker
231,007 Points

I would expect the error is coming from this line:

    for card in game.set_cards:

Since "set_cards" is defined as a method of the Game object, it is not usable as the iterable in the for loop.

Did you perhaps mean to loop on game.cards instead?