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 trialSydnie Gandy
3,024 PointsMy code isn't printing out the cards' words correctly whenever you make a guess.
Hey there! My code isn't printing out the words on the cards correctly whenever you make a guess. It's supposed to tell you what was on the back of both cards, but it only ever prints one. It makes the game hard to complete. I feel like I might have overlooked something but I can't seem to find it...
Here's my code:
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):
# | A | B | C | D |
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
def check_win(self):
for card in self.cards:
if card.matched == False:
return False
return True
def check_location(self, time):
while True:
guess = input(f"What's the location of your {time} card? ")
if guess.upper() in self.locations:
return guess.upper()
else:
print("That's 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()
Mod edit: added markdown formatting for code
Cameron Childres
11,820 PointsHey Sydnie,
To display your code cleanly in the community you can put a line of three backticks before and after the code block. If you add a language name after the first three backticks it will add highlighting. Here's an example:
```python
print("Hello World")
```
Will display as:
print("Hello World")
I've gone ahead and formatted the code in your post. For more examples of ways to format things in the community, check out the "markdown cheatsheet" linked below the comment box.
Cameron Childres
11,820 PointsI just ran your code in workspaces and this is the output it gave me:
What's the location of your first card? c1
What's the location of your second card? c4
| A | B | C | D |
1| | | Boo | |
2| | | | |
3| | | | |
4| | | Boo | |
What's the location of your first card? c2
What's the location of your second card? c3
| A | B | C | D |
1| | | Boo | |
2| | | Hut | |
3| | | Hut | |
4| | | Boo | |
Would you mind sharing a workspace snapshot so I can take a closer look to see what's going on? To post a snapshot:
- Click the camera icon in the top right of your workspace
- Click "take snapshot"
- Follow the link it generates (opens in new window)
- Copy and paste link from address bar here
1 Answer
Cameron Childres
11,820 PointsHi Sydnie,
I misunderstood your question at first, sorry about that! I assumed that you were having trouble with them both displaying when a correct guess was made, but see now that you're talking about showing both when an incorrect guess is made.
The issue comes from your check_match function:
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
Take a look at your "else" statement. With the return included in the "for" loop it will break before it has a chance to loop the second time. To fix this you just need to remove one level of indentation from "return False":
else:
for card in cards:
print(f'{card.location}: {card}')
return False
This way the loop can complete before the return executes.
Hope this helps! Let me know if you have any questions.
Sydnie Gandy
3,024 PointsSydnie Gandy
3,024 PointsSorry, it seems the code didn't print out correctly in my question, either.