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

Can't figure out how to fix this matching game

Every time I run the code, this prints out, but it should be a nice grid.

'''python class Card: def init(self, word, location): self.card = word self.location = location self.matched = False

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

def __str__(self):
    return self.card

if name == 'main': card1 = Card('egg', 'A1') card2 = Card('egg', 'B1') card3 = Card('hut', 'D4')

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

from cards import Card import random

class Game: def init(self): self.size = 4 self.card_options = ['Add', 'Boo', 'Cat', 'Dev', 'Egg', 'Far', 'Gum', 'Hut'] 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 word 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(word, random_location)
        self.cards.append(card)

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 

def create_grid(self):
    header = ' |  ' + '  |  '.join(self.columns) + '  |'
    print(header)
    for row in range(1, self.size + 1):
        print_row = f'{row}| '
        get_row = self.create_row(row)
        print_row += ' | '.join(get_row) + ' |'
        print(print_row)

def check_match(self, loc1, loc2):
    cards = []
    for card in self.cards:
        if card.location == loc1 or card.location == loc2:
            cards.append(card)
    if cards[0] == cards[1]:
        cards[0].matched = True
        cards[1].matched = True
        return True
    else:
        for card in cards:
            print(f'{card.location}: {card}')
        return False    

if name == 'main': game = Game() game.set_cards() game.create_grid()

#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))

'''

3 Answers

Try changing

def set_cards(self):
    used_locations = []
    for word 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(word, random_location)
        self.cards.append(card)

to

def set_cards(self):
    used_locations = []
    for word 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(word, random_location)
            self.cards.append(card)

so that each word is added twice instead of only adding each word once.

don't know why the code came out like that, it's kind've hard to read, sorry

| A | B | C | D |
1| |
2| | |
3| | |
4| | | |

this is what prints out