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

Rolando Aguilera
Rolando Aguilera
4,662 Points

Problem in the above code, unable to pass this challenge! Works perfectly on workspace...

Score method is created on the Game class, and asks the user to enter 1 or 2 and the number is summed to the self.current_score list! But unable to pass the challenge, any help will be appreciated!! Thanks!!

game.py
class Game:

     def score(self):
        player = int(input("Type 1 or 2! "))
        if player == 1:
            self.current_score[0] = self.current_score[0] + 1
        elif player == 2:
            self.current_score[1] = self.current_score[1] + 2
        else:
            return self.score()

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

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Rolando,

Your logic is on the right track but the reason it is not passing the challenge is because you are adding in some extra things that the challenge did not ask for.

First, the challenge does not ask you to accept an input from a user.

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

So the score method will need to have an argument named "player" that will be passed in. You will need to account for that in your method definition. By adding a user input you are effectively overriding the "player" value that the code checker is trying to pass in.

Secondly,

Increase that player's value in self.current_score by 1.

The score method is used to increment each player's score by one. The only condition to be tested is which player you need to increment (which you are doing correctly).

But notice your handling of Player 2. You are incrementing Player 2's score by two.

And lastly, the challenge doesn't ask you to handle an else condition that calls itself. In this case, it won't affect whether it passes or not, but going outside the scope of the challenge instructions will usually cause problems.

Hope this helps.