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

peter hampton
peter hampton
18,068 Points

Adding a score method to Game

I am having a little trouble with an OOP code challenge. I can't get the list value to increment by one, but why?

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

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

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

self.current_score[2] isn't going to work since that's out of range. Also self.score() is going to blow up because you didn't provide a player and there's no default. The method doesn't need to return anything either. Once I cleared these things up, your example code passed.

There's an easier way, though. Since player is a number (1 or 2), just decrease it by 1 and then do something like self.current_score[player-1] += 1 (assuming you didn't decrease the player number into a new variable).

peter hampton
peter hampton
18,068 Points

Facepalm! The Index was exactly the problem and thank you for the advice with formatting list indexes!