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 trialAndrew Ge
2,158 PointsCode Not Working
I simply don't know what is happening
Andrew Ge
2,158 PointsHere is the code:
class Game:
def __init__(self):
self.current_score = [0, 0]
def score(player):
if player == 1:
self.current_score[0] = self.current_score[0] + 1
else:
self.current_score[1] = self.current_score[1] + 1
[MOD: added ```python markdown formatting -cf]
2 Answers
Chris Freeman
Treehouse Moderator 68,454 PointsYou are very close. You need to add the instance reference to the method signature:
class Game:
def __init__(self):
self.current_score = [0, 0]
def score(self, player): # <-- added 'self'
if player == 1:
self.current_score[0] = self.current_score[0] + 1
else:
self.current_score[1] = self.current_score[1] + 1
Andrew Ge
2,158 PointsWow.
Sigh. Compilers are so picky.
Also, I just wanted to know if I am asking in the right manner; is the community okay with me simply posting my questions when I can't solve them, or are more detailed, technical problems what is handled in the forums?
Chris Freeman
Treehouse Moderator 68,454 PointsHi Andrew, your question is great! The best questions are those that say "here is where I'm stuck. What am i doing wrong or what is the next step?". We all make the same mistakes and these questions allow help other students to challenge their knowledge trying to solve it and see how more experience programmers may solve it.
There are some questions posted by others that don't state what problem they are having or show what they used to try and find a solution. These can be frustrating because the lesson learned is not obvious. Some say "just give me the answer to X". These get answered but aren't in the community spirit.
Seeing how and why others are making mistakes and solving them is the best method to learning.
Thanks for posting to the forum! Keep up the good work!!
Darryn Smith
32,043 PointsJust as a point of discussion, the following version of the method works as well, but in a single line of code:
def score(self, player):
self.current_score[player-1] += 1
For the record, it wasn't so long ago I wouldn't have thought of that either.
Also for the record, I'm here in the first place because I had the same problem as the OP: I didn't know to include 'self' as an argument to the method definition. Thanks to Mr. Freeman for helping us out.
Andrew Ge
2,158 PointsAndrew Ge
2,158 PointsAnd to Treehouse: even though the "include code in comment" box is checked, the code does not appear.