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 trialronakeogh
30 PointsWhats wrong with my code? Help much appreciated!
e
from game import Game
class GameScore(Game):
pass
def __str__(self):
return("Player 1: {}; Player 2: {} ".format(self,score))
3 Answers
Jennifer Nordell
Treehouse TeacherHi there! The problem is in your return string. First, you have some extra spacing that it doesn't want, but also self.score is a tuple that is being used. So we have to access the different items in the tuple. Here's the line I used for the string that's being returned:
return("Player 1: {}; Player 2: {}".format(self.score[0], self.score[1]))
Here we take self.score at the index of 0 which is player 1's score and insert it into the first position, and self.score at the index of 1 and insert it into the second position.
Hope this helps!
Steven Parker
231,271 PointsJennifer's solution is correct, but you can also make use of the shorthand for unpacking the tuple:
return("Player 1: {}; Player 2: {}".format(*self.score))
Also, you don't need pass anymore once you add some code to your class.
ronakeogh
30 PointsThanks much appreciated
Troy Riggs
13,859 PointsThere are several problems with your code.
The main problem is probably that you need to reference Game's score variable, which you do with a period instead of a comma.
self.score
Also, since you have Player 1 and Player 2, you will need two variables in format. example:
'Player 1: {}, Player 2: {}'.format(self.score1, self.score2)
I'm assuming score1 and score2 are integers representing Player 1 and Player 2 respectively. If your score variable is a collection, you can just use indexes, such as:
'Player 1: {}, Player 2: {}'.format(self.score[1], self.score[2])
In the code you have showing, you don't need pass
and you don't need to wrap the returned string with parenthesis. Although they are not impacting how the code compiles and runs, they don't improve human readability either.
ronakeogh
30 PointsThanks very much problem solved
ronakeogh
30 Pointsronakeogh
30 PointsThanks thats the problem solved :)