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

John Hollier
John Hollier
902 Points

problem with stage 2 inheritance challenge not sure what its asking me to do.

Challenge task 1 of 1 Add a score method to Game that takes a player (1 or 2) and increases that current_score member's score by 1. Bummer! Try again! Preview Recheck work game.py

class Game:

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

  def score(self):
    self.current_score = [+1, 0]
John Hollier
John Hollier
902 Points
class Game:
  def __init__(self):
    self.current_score = [0, 0]

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

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi John,

Your above code isn't working because the value +1 isn't a valid incremented integer, you're also missing the player parameter from your score method to target the correct index in the current_score array.

Below is the the full code but I'll explain it afterwards.

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

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

So the first difference is there's now a parameter called player which is an integer value as per the task question, this value can either be 1 or 2, the next difference is we're only targeting the player in the array using the player parameter set but we're also deincrementing the value of player by 1 because arrays always start at index 0.

Finally we increment the value of the players score by one and we're done.

Hope that helps.

could you explain the player part a little more in detail? ie if we input player 1 would it be player 1 (0-1=-1 index position minus 1 = -1 THEN += 1 so back to 0? but what about player 2 ? 1-1=0 then +=1 so back to 1...? shouldnt they both be equal? I guess I am missing something here or not understanding it properly

Chris Shaw
Chris Shaw
26,676 Points

Hi Jamison,

I think you're confusing what the code is actually doing, I'll break it down.

# This refers to the players array, both players start with a score of zero
self.current_score

# The following is how we get the current score for the specified player index, this mini game
# is based on the logic that players start at 1 thus we need to minus one because arrays start
# at zero
[player - 1]

# Finally we can use the increment operator to add one to the players score, this operator also
# works for concatenating strings so don't see it as just a way to increase number values
+= 1

If we now run the code as per the below example (not a working example) we can modify each players score and get a different result depending on the player we've selected.

Game.score(1) # results in player 1 getting a score of 1

Game.score(2) # results in player 2 getting a score of 1
Game.score(2) # results in player 2 getting a score of 2

Hope that helps.