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__

Dawid Cedrych
Dawid Cedrych
15,108 Points

Task 1 no longer passing in code challenge: __str__

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

I can't figure it out what is wrong here, please give me some advice

Thanks in advance

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Looks like you didn't indent the body of the function.

from game import Game
class GameScore(Game):
    def __str__(self):
        self.score = (5, 10)
        return "Player1: {}; Player2: {}".format(self.score, self.score)
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Well, self.score is a tuple, so you're going to end up with `"Player1: (5, 10); Player2: (5, 10)", which isn't what you want. How do you get to the members of a tuple/list?

my code is pass :)

from game import Game class GameScore(Game): def str(self): self.score = (5, 2) return "Player 1: {}; Player 2: {}".format(self.score[0], self.score[1])

This solved the code challenge for me, but for some reason, I don't think that it was the way it was supposed to work, as the code challenge say "self.score is a tuple with Player 1's score and Player 2's score like (5, 10)." Please give me some hints to help me solve it correctly.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Rune Larsen Ideally, you'd just unpack the tuple into the .format() call. Something like:

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

Also, you don't need to define self.score. It comes from the Game class.