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 2

Artithaya Bachner
Artithaya Bachner
1,562 Points

Code just prints out [] with nothing in it

from cards import Card 
import random 



class Game:
    def __init__(self): #grid size
        self.size = 4
        self.card_options = ['dog', 'cat', 'egg', 'tea', # card options 
                             'wii', 'key', 'sun', 'oil']
        self.columns = ['A', 'B', 'C', 'D'] # columns
        self.cards = [] # size 
        self.locations = [] # 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 word in self.card_options:
            for i in range(2):  # because each card appears twice
                available_locations = set(self.locations) - set(used_locations)
                random_location = random.choice(list(available_locations))
                used_locations.append(random_location)
                card = Card(word, random_location)
                self.cards.append(card)





# methods 

    def create_row(self, num): 
        row = []
        for column in self.columns:
            for card in  self.cards:
                if card.location == f'{column} {num}':
                    if card.matched: 
                        row.append(str(card))
                    else:
                        row.append('   ')
        return row 

    # create cards 
    # create grid 
    # check for matches
    # check if game has been won
    # run the game

# dunder main 
if __name__ == '__main__': 
    game = Game()
    game.set_cards()
    game.cards[0].matched = True 
    game.cards[1].matched = True 
    game.cards[2].matched = True 
    game.cards[3].matched = True 
    print(game.create_row(1))
    print(game.create_row(2))
    print(game.create_row(3))
    print(game.create_row(4))
    #create game instance

1 Answer

Steven Parker
Steven Parker
242,015 Points

I see two issues:

# line 16:
               self.locations.append(f'{column}{num})')
#                                                   šŸ‘†šŸ»
#                                                   this adds a spurious ")" to the string
# and line 38:
                if card.location == f'{column} {num}':
#                                             šŸ‘†šŸ»
#                                             this adds a space between the location parts

Either one of these will cause the location matching to fail.

Artithaya Bachner
Artithaya Bachner
1,562 Points

oh god thank u sm!! it really are the small things hahaha