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__

luke hammer
luke hammer
25,513 Points

Python inheritance question

This is the question form the test. Add a str method to GameScore that returns the score in the string "Player 1: 5; Player 2: 10", using the correct values from self.score. self.score is a tuple with Player 1's score and Player 2's score like (5, 10). You do not need to define self.score. It comes from the Game class.

I don't know how to debug and see what is happening wrong here.

game_str.py
from game import Game
class GameScore(Game):
  def __str__(self):
    score = self.score.self.score
    message = "Player 1: {0} Player 2: {1}".format(score[0],score[1])
    print("this is a test")
    return message
luke hammer
luke hammer
25,513 Points

cool got it

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

3 Answers

You don't need to redefine score there, and if you were to that's not how you would do it. self.score doesn't have a property called "self" so that's not going to succeed. However, the format logic looks sound.

luke hammer
luke hammer
25,513 Points

So i attempt this but this also does not work.

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

You're still doing:

self.score.self.score

Just do:

self.score
luke hammer
luke hammer
25,513 Points

Cool got it!! thank you !!

Jeremy Kerrigan
Jeremy Kerrigan
12,002 Points

I solved it like this by unpacking the tuple into 2 separate variables x, y

from game import Game

class GameScore(Game):
  pass

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

I still could not solve this problem #kenneth