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

Instance Methods Challenge

The output keeps saying "Bummer! Try Again!" so I can't really figure out where my code goes wrong.

I'm supposed to create a score method that'll increase the current score by 1 depending on the selection. I'm not sure what is wrong with my code. Thanks!

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

  def score(self):
    score_choice = input('[1] or [2]').astype(int)

    if score_choice == 1:
      self.current_score[0] = 1
    else:
      self.current_score[1] = 1

Kenneth Love

This code challenge is passing with the correct code but it's also passing with code that always sets the score to 1.

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Add a score method to Game that takes a player argument that'll be either 1 or 2.

The challenges states that you need to pass in the required argument player to the score method, but your method doesn't have it. And there're other issues as well.

  • score_choice = input('[1] or [2]').astype(int) you do not need this line, because we aren't asking user for input here.
  • if score_choice == 1: the condition should be checking against the player argument.
  • self.current_score[0] = 1, you need to increment the first item of self.current_score by 1, NOT assign 1 to it; same goes true for self.current_score[1] = 1
class Game:
  def __init__(self):
    self.current_score = [0, 0]

  def score(self, player):
    if player == 1:
      self.current_score[0] += 1  # increase the 1st item in the self.current_score list by 1
    if player == 2:
      self.current_score[1] += 1  # increase the 2nd item in the self.current_score list by 1

Hope it helps.

The score method could also be a 1-liner since the index to be incremented is always 1 less than the player number. You can simply increment the index that is player - 1

def score(self, player):
  self.current_score[player - 1] += 1
Tony McCabe
Tony McCabe
4,889 Points

This is the answer which passes the challenge thanks