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 trialAndy McDonald
Python Development Techdegree Graduate 13,801 PointsWhy 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
Treehouse Moderator 68,457 PointsHey 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!!!
Andy McDonald
Python Development Techdegree Graduate 13,801 PointsAndy McDonald
Python Development Techdegree Graduate 13,801 PointsThat makes sense. Thank you!