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

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!