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

Code Not Working

I simply don't know what is happening

And to Treehouse: even though the "include code in comment" box is checked, the code does not appear.

Here is the code:

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
    else:
      self.current_score[1] = self.current_score[1] + 1

[MOD: added ```python markdown formatting -cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,986 Points

You are very close. You need to add the instance reference to the method signature:

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

  def score(self, player):  # <-- added 'self'
    if player == 1:
      self.current_score[0] = self.current_score[0] + 1
    else:
      self.current_score[1] = self.current_score[1] + 1

Wow.

Sigh. Compilers are so picky.

Also, I just wanted to know if I am asking in the right manner; is the community okay with me simply posting my questions when I can't solve them, or are more detailed, technical problems what is handled in the forums?

Chris Freeman
Chris Freeman
Treehouse Moderator 67,986 Points

Hi Andrew, your question is great! The best questions are those that say "here is where I'm stuck. What am i doing wrong or what is the next step?". We all make the same mistakes and these questions allow help other students to challenge their knowledge trying to solve it and see how more experience programmers may solve it.

There are some questions posted by others that don't state what problem they are having or show what they used to try and find a solution. These can be frustrating because the lesson learned is not obvious. Some say "just give me the answer to X". These get answered but aren't in the community spirit.

Seeing how and why others are making mistakes and solving them is the best method to learning.

Thanks for posting to the forum! Keep up the good work!!

Darryn Smith
Darryn Smith
32,043 Points

Just as a point of discussion, the following version of the method works as well, but in a single line of code:

  def score(self, player):
    self.current_score[player-1] += 1

For the record, it wasn't so long ago I wouldn't have thought of that either.

Also for the record, I'm here in the first place because I had the same problem as the OP: I didn't know to include 'self' as an argument to the method definition. Thanks to Mr. Freeman for helping us out.