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 3

Guillaume Crt
Guillaume Crt
6,395 Points

need clarification about return statement in check_win method

my question is about the check_win method :

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

if the return False appears then the rest of the code (return True) is not running and the loop is ending ?

I would have write :

{
def check_win(self):
        check_win = True
        for card in self.cards:
            if not card.matched:
                check_win = False
                break
            else:
                continue

        return check_win
}```

3 Answers

Hi Guillaume!

I think both approaches achieve the same end (although I didn't test them), but I would have to argue the original code is succinct and difficult to improve.

The main point is that you want to loop through all the cards and as soon as you find a card, if any, that hasn't been matched, you return false immediately (because the game isn't won/finished yet). In other words, once one card has been identified as not matched, there's no point in checking the rest.

And yes, any time you return from a function, any remaining code in that function is bypassed.

I hope that helps.

Stay safe and happy coding!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Guillaume Crt, if I may add a bit more....

You may have heard that there is a "Pythonic" way to code, though even in this, there are conflicting camps. Some believe a function should have a single return, others believe that multiple returns are ok. I say it depends on if the multiple returns add or detract from the solution.

In the second solution, it might be confusing to have a variable with the same name as the function. A suggestion here would be to use a name that connotes a winning state such as "win".

A continue is never needed as the last statement in a block since the end of a block will always return to the top. With out the need for the continue, the else is also no longer needed.

Putting these together yields a solution as good as the multiple return version.

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

Kudos on using code block styling. In addition to css, you can use python (as I did above)!

Hey Chris!

Useful input.

I certainly advocate using many recourses/every resource available, so thank you!

-Pete

Guillaume Crt
Guillaume Crt
6,395 Points

thanks a lot for those explanations !