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 Basic Object-Oriented Python Creating a Memory Game Game Class Part 1

Andy McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andy McDonald
Python Development Techdegree Graduate 13,801 Points

Why does instance in list have no attribute?

I'm going over the lesson game.py lesson in the OOP Matching Game lesson. https://teamtreehouse.com/library/game-class-part-1#questions

When I create the function set cards:

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

Then while trying to check to see if there is a separate location attached to each instance. I did the following:

game = Game()
game.set_cards()

for i in game.cards:
    print(i)
    print(game.cards.location)

But got the error: AttributeError: 'list' object has no attribute 'location'

Haven't I loaded the list with instances that should have a location?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Hey Andy McDonald, the object game.cards is the list created above from self.cards If you’re trying to access the location of each card in the loop, use i.location

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