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 trialAbhishek Gangadhar
9,467 PointsCant Find why I'm not getting through this challenge
I need to increment the current_score with respect to the input of player. Not understanding what i did wrong
class Game:
def score(self,player):
if player in [1,2]:
if player == 1:
self.current_score[0] += 1
else:
self.current_score[1] += 1
else:
self.score()
def __init__(self):
self.score(self.player)
self.current_score = [0, 0]
3 Answers
Josh Keenan
20,315 Pointsclass Game:
def __init__(self):
self.current_score = [0, 0]
def score(self, player):
if player == 1:
self.current_score[0] += 1
elif player == 2:
self.current_score[1] += 1
else:
pass
Here's my code to the challenge, it works as it's simple Keep code simple! You can simply test to see if the argument is 1 or 2, yours is probably fine except for the last else statement, you don't need it to be recursive and the player argument isn't passed into it then!
Chris Freeman
Treehouse Moderator 68,454 PointsHi Abhishek, your original code is very close to correct. Remove that last else
:
class Game:
def score(self,player):
if player in [1,2]:
if player == 1:
self.current_score[0] += 1
else:
self.current_score[1] += 1
# This else is unnecessary. If 'player' not 1 or 2 do nothing.
#else:
# self.score()
def __init__(self):
self.score(self.player)
self.current_score = [0, 0]
Abhishek Gangadhar
9,467 PointsThank you
Josh Keenan
20,315 PointsNo you don't, this is because the challenge is simply to create the function, the code challenge is just to test you understand the principles of the last video's lesson. In later challenges you may need to call the methods, but don't do so unless it explicitly asks you to do so!
Abhishek Gangadhar
9,467 PointsAbhishek Gangadhar
9,467 PointsHey Josh, Your code worked, But dont we need to call the function in order for it to work?