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__

Milagros Roman
Milagros Roman
1,228 Points

I can't continue. it says that the Task 1 is no longer passing!!

TASK 1 passes: from game import Game class GameScore(Game): pass

TASK 2 doesn't pass but I really don't know what to do here even if I erase the world 'pass" from de code from game import Game class GameScore(Game): pass def str(self): returns "Player 1:{}; Player 2:{}".format(self.score, self.score)

game_str.py
from game import Game
class GameScore(Game):
  pass
  def __str__(self):
    returns "Player 1:{}; Player 2:{}".format(self.score, self.score)

3 Answers

Dan Johnson
Dan Johnson
40,532 Points

Since you don't need a placeholder anymore, you can get rid of pass. You also want to change returns to return.

The instance variable self.score is a tuple, so if you want to use it without selecting the indexes you will have to unpack it:

def __str__(self):
    # Unpack the scores in to two positional arguments.
    return "Player 1: {}; Player 2: {}".format(*self.score)
Milagros Roman
Milagros Roman
1,228 Points

Thank Dan, You are the king!! I don't understand a lot about unpacking, however I think I will learn it step by step... I hope I understood your suggestion and it worked but I need practice with this. Thank you again Mila

Dan Johnson
Dan Johnson
40,532 Points

Unpacking a tuple basically turns it into some number of positional arguments. For example:

# If we have score defined like this
score = (5, 10)

# Then these are all functionally equivalent
print( "Player 1: {}; Player 2: {}".format(*score) )
print( "Player 1: {}; Player 2: {}".format(score[0], score[1]) )
print( "Player 1: {}; Player 2: {}".format(5, 10) )

The last line is what you can imagine score looks like after unpacking.