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

Efe Mirkan Guner
Efe Mirkan Guner
2,413 Points

card.matched

We have lopped through the cards in self.cards then checked if card.location == f'{column}{num} . then why did we use if card.matched again ?

2 Answers

Hello Efe!

I am guessing this is the part that you have problem with:

  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

Lets take this step by step:

for card in self.cards:
  if card.location == f'{column}{num}':
    if card.matched:
      row.append(str(card))
    else:
      row.append('   ')
  1. First we loop through the card deck
  2. If the card location is equal to the current position in the loop (which it will be everytime since every card is set to a location in the set_cards method).
  3. A second nested if statement determines if the card should be visible or not (if the current card objects instance attribute self.matched is set to True then the cards str dunder gets called and passed to the row.append() function.
  4. If the current card objects instance attribute matched is set to False (which it is when its init method is called) then row.append() will get ' ' appended to it since we don't want to show the player the card.

So the answer to your question is that we use if card.matched is just to determine if the player has guessed two or more cards right and then we would want the player to see these cards the next round.

Maybe you already understand this but this:

if card.matched

is the same as this:

if card.matched == True

If something feels unclear, don't hesitate to ask me again :)