Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python (retired) Inheritance Score Method

Help me out, what am I missing?

There is obviously something wrong and I quite cant put my finger on it.

game.py
class Game:
    def __init__(self):
        self.current_score = [0, 0]

    def score(player):
        if player == 1:
            self.current_score[0] = self.current_score[0] + 1
        elif player == 2:
            self.current_score[0] +=1
        else self.current_score = self.current_score

1 Answer

cm21
cm21
150,854 Points

Hi Mduduzi,

1) The error we get from this code is 'Bummer! SyntaxError: else self.current_score = self.current_score'. We can start by taking out that line, since even the compiler did allow it to run, all we would be doing is setting current_score to itself, which it already theoretically would be.

2) After we do that, we get the generic 'Bummer! Try again!' error. The reason for this is because we are not allowing 'self' to be passed to the score method, in our method declaration. The 'self' parameter has to be written before the 'player' parameter in the function header (if we are going to operate on 'self', we need to immediately state it as a parameter in the method header.)

3) Finally, we get an error message of 'Bummer! Score didn't increase correctly. Expected [0, 1], got [0, 0].' This suggests that our code theoretically would be compiled and run, but we aren't getting the result that we're logically looking for. The reason for this is because, in our elif clause, we still seem to be updating self.current_score[0]. Since we are updating the current_score for player 2, that index should be a 1 instead.

class Game:
    def __init__(self):
        self.current_score = [0, 0]

    def score(self, player):
        if player == 1:
            self.current_score[0] += 1
        elif player == 2:
            self.current_score[1] += 1

Hope this helps!

Thankyou