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__

_str_(self) Task 2

I am not sure why this is not passing?

game_str.py
from game import Game

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

3 Answers

Hara Gopal K
PLUS
Hara Gopal K
Courses Plus Student 10,027 Points

this would also work: .format(*self.score) # unpacking the tuple

Nathan Tallack
Nathan Tallack
22,159 Points

You ALMOST had it! Great work by the way!

You are refernecing the tuple at index 0 and index 1 to get the values 5 and 10.

So your code would look like this.

from game import Game

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

Keep up the great work! :)

I changed the index values to 0 and 1 as you suggested and it passed (Thank you!). I'm still wondering why my code no longer needs the result of a 5 score and a 10 score. Did the question just want us to write a general code and was just providing an example of what the results would be if it was actually carried out?

The question was:

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.

Nathan Tallack
Nathan Tallack
22,159 Points

Yeah, the question reads a little ambitiously. The giveaway there is the use of the tuple. So that tells you that you are wanting to use the index numbers to reach the values within that tuple. :)

Paul Heneghan
Paul Heneghan
14,380 Points

Thanks to Nathan, this helped me, too. I actually had the right answer for the str method, with the indexes 0 and 1, but left the pass statement in from the first challenge. Once I removed it, my code passed.