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__

Code challenge formatting a class method with a tuple.

So I'm obviously missing something, but I have no idea what. It gives you a tuple that gets pulled from Game, and then asks you to format string for GameScore with that tuple. I've done that, and have tried putting 'pass' before and after str block, and omitting it to no avail. What's missing (Bummer! Try again! error)

game_str.py
from game import Game

class GameScore(Game):

    def __str__(self):
        return "Player 1: {}; Player 2: {}".format(self.score)

2 Answers

the self.score argument you are feeding to the format method is improper. the tuple has two scores in it, player 1 and player 2 (presumably) and you have spaces in your return string for two scores to go, but you then point to the whole tuple, not its elements, so it throws an error. try to find a way (with brackets, []) to reference the individual elements in the tuple such that they go where they are intended to go in the formatted return string.

Ahhh, so point it in the right direction with index placements! Thank you! I can't believe I didn't think of that...

Hi Garrett, self.score is a tuple so you need to make sure you are calling the format method with the right number of arguments, not the single tuple.

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

Or if you have taken the Python collections course, the code below may be of interest :)

class GameScore(Game):
    def __str__(self):
        return 'Player 1: {}; Player 2: {}'.format(*self.score)

Just a single star, not **? Collections seems to have disappeared while I was in the middle of it.