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 Object-Oriented Python (retired) Inheritance Score Method

Add a score method to Game that takes a player argument. The player argument will be either 1 or 2. Increase that player

I have an answer to this challenge, but I'm NOT solid on grasping it. I have my weak understanding and questions in the comments. Thank you. BTY, my terminology isn't precise. Please overlook that or correct me, either way.

game.py
class Game:
# I think I've heard reference that init allows us to
# override things.  What things are being overridden here? 
    def __init__(self):
        self.current_score = [0, 0]
# method: self in mandatory? player is the "active" parameter?
    def score(self, player):
# player is one of two players are being passed in?
# it could be player 1 or player 2?
# or, player is the current_score var?
# or current score holds the value of player 1& 2?
        if player == 1:
# if player one scores add a point
            self.current_score[0] += 1
        elif player == 2:
# if player two scores add a point
            self.current_score[1] += 1

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! While I believe things can be overridden in the init function, that's not really happening in this instance. An init method is often required when making a class definition to give the default property values of the object being created. So in this instance, we're saying what should happen when a new instance is created and defining the score method to increment a score.

Let's say, for example, that you can run this game over and over and keep statistics. That's a little more advanced than what's happening here, but bear with me. Every time we start a new game, we create a new Game object. When that happens the init runs, which sets the score to 0 for both players. This makes sense as we want everyone to start with the same score.

Now, we have def score(self, player):. This defines the method that increments the score for the player who got the point. So we pass in self, which refers to the current instance of the Game object we're working with. And an example of code that might fit this is: game17.score(1). The score would increment for player 1 for game17 and no other instances of Game. The rest you seem to have a pretty good grasp on!

Hope this helps! :sparkles:

Thank you Jennifer