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

Overwhelming and Code Debugging

Hello everybody,

Feedback: I have to say this video was the reason I stopped learning python almost a year ago. It simply was overwhelming and there are many concepts presented in an 'eat or die fashion'. Thus, I have to ask you people from treehouse to provide further material before this project or just leave it. For now I have restarted the course and feel fine without understandig all of it - it is impossible to understand with the information given by team treehouse.

Question: As soon as I have matching cards, my console returns this :

Traceback (most recent call last):
File "/home/treehouse/workspace/game.py", line 104, in <module>
game.start_game()
File "/home/treehouse/workspace/game.py", line 81, in start_game
if self.check_match(guess1, guess2):
File "/home/treehouse/workspace/game.py", line 51, in check_match
cards.matched == True
AttributeError: 'list' object has no attribute 'matched'

Can anybody explain to me where the problem is?

Thank you very much. And I hope my honest feedback is appreciated

cards.py:

  .fun {
    background-color: yellow;
    color: pink;
    font-size: 5000px;
  }

class Card():
    def __init__(self, name, location):
        self.name = name
        self.location = location
        self.matched = False

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

    def __str__(self):
        return f'{self.name}'

if __name__ == '__main__':
    card1 = Card('egg', 'A1')
    card2 = Card('hut', 'B4')
    card3 = Card('egg', 'C3')

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

game.py:

  .fun {
    background-color: yellow;
    color: pink;
    font-size: 5000px;
  }
from cards import Card
import random

class Game:
    def __init__(self):
        self.size = 4
        self.card_options = ['Add', 'Boo', 'Egg', 'Hut', 'Tab', 'Nut', 'Woo', 'Top']
        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 name 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(name, 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.matched == True
            return True
        else:
            for card in cards:
                print(f'{card.location}: {card}')
            return False

    def check_win(self):
        for card in self.cards:
            if card.matched == False:
                return False
        else:
            return True

    def check_location(self, time):
        while True:
            guess = input(f"What's your loction of your {time} card?")
            if guess.upper() in self.locations:
                return guess.upper()
            else:
                print("This is not a valid location. It should look like this A1")

    def start_game(self):
        game_running = True
        print('Memory game')
        self.set_cards()
        while game_running:
            self.create_grid()
            guess1 = self.check_location('first')
            guess2 = self.check_location('second')
            if self.check_match(guess1, guess2):
                if self.check_win():
                    print('Congrats! You have guessed them all!')
                    self.create_grid()
                    game_running = False
            else:
                input('Those cards are not a match. Press enter to continue')
        print('GAME OVER')

if __name__ == '__main__':
    game = Game()
    game.start_game()

1 Answer

Steven Parker
Steven Parker
231,248 Points

The last part of the Traceback output is telling you where the problem is. The line "File "/home/treehouse/workspace/game.py", line 51, in check_match" tells you which file, line, and method to look for.

Then, "cards.matched == True" is the actual problem line. And "AttributeError: 'list' object has no attribute 'matched'" is pointing out that "cards" is a list, and it has no attribute named "matched" that you can assign to (even if it contains objects that do have that attribute).

My guess is what you really meant to do was to set the "matched" property on each of the individual card objects that were just compared.