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 Instance Methods

omri golan
PLUS
omri golan
Courses Plus Student 15,836 Points

class challange

this is the challange i dont understand what am doing wrong can you help me?

Add a score method to Game that takes a player argument. The player argument will be either 1 or 2. Increase that player's value in self.current_score by 1. You'll need to adjust the index (i.e. player = 1 means self.current_score[0] needs to increase).

class Game:

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

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

      else:
        return self.current_score[1] += 2

thanks in advance

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

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,082 Points

There are two issues. The task asks only to update the values not return them and the score increment for player two should also be 1:

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

    def score(self,player):
        if player == 1:
            self.current_score[0] += 1  # <-- removed return
        else:
            self.current_score[1] += 1 # <-- changed to 1, removed return

Also, avoid mixing tabs and spaces.

Evan Colvin
Evan Colvin
1,190 Points

Thanks! That helped a lot!

Quick question: without a return statement, how does the score function actually do anything?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,082 Points

The score function is bound to an instance of Game(). The self argument represents the specific instance. Since it operates on self.current_score it is changing a particular instance attributes and doesn't need to return anything.

These bound functions are called methods. Methods can return objects. score() does not. The default return if not stated is return None

Richard Nesbitt
Richard Nesbitt
6,775 Points

I completed this challenge in a simpler fashion:

def score(self, player):
  self.current_score[player - 1] += 1 #makes the index out of the player number
Matt Nickele
Matt Nickele
468 Points

class Player:

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

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

  else:
      self.current_score[1] =+ 2
      return self.current_score

def __str__(self):
    #This is a print function
    return str(self.current_score)

a = Player() b = Player.score(a,1) print (a)

Here is what I did. You need a print statement str so you can print your created object and not just the python name for it that means nothing. I personally like to make chances before returning but that is a choice. I hope this helps