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 __str__

Whats wrong with my code? Help much appreciated!

e

game_str.py
from game import Game

class GameScore(Game):
    pass
    def __str__(self):
        return("Player 1: {}; Player 2: {} ".format(self,score))

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi 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! :sparkles:

Thanks thats the problem solved :)

Steven Parker
Steven Parker
231,271 Points

Jennifer'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.

Thanks much appreciated

There 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.

Thanks very much problem solved