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__

confused what to ans and how..

how to write the code for this..

game_str.py
from game import Game
class GameScore(Game):
  def __str__(self):

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points
from game import Game

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

This should do it.

thnks....bt can u plz explain the last line...i.e the 0 and 1 in between {} and the (*self.score)

William Li
William Li
Courses Plus Student 26,868 Points

yes, 0 and 1 just means select the 0th, and 1st item from self.score, You need to put * before the self.score to get it to work; I'm not sure if you've seen Kenneth lectures on collection packing and unpacking yet, if this is beyond your current progress, then it probably doesn't make too much sense right now. Sorry about that, Perhaps the following solution works better for you in terms of readability?

from game import Game

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

thnk u..ya ya got it mostly as ..similar to array?? ryt?...can u plzz tell me the order in which i shall watch these vdos to get max of them...

William Li
William Li
Courses Plus Student 26,868 Points

I think as long as you're following the course along in Python track, you're set. Because the fault is mine in this case, sometimes when answering a student's question, I make use of techniques or concepts, without realizing, they haven't learned yet, that confused them. I try to avoid doing so as much as possible now, but sometimes just by looking at the question, it's not easy to figure out how much knowledge of Python a student has learned.

its okey okey..i partially got that..and was just confused...m very begnr to python..only compltd one module yet thats why .. its also not possble for u to guess how much i know.. (y)