Building Useful Classes - Why is this wrong?


I don't understand why this is wrong, someone please help.


Indentation error.

def repr(self):

should line up with,

def __init__(...):


ok, so I fixed the indentation error but the code is still wrong. @mtf


Anything after return is unreachable. The last two lines above should not be inside the class definition. my_point is an instance of the class. Remove all indentation on those lines.


did u mean like this? @mtf


Yes, like that. Did you get the expected print out? I'm not sure that the representation is as expected.

return "(%d, %d, %d)" % (self.x, self.y, self.z)

should be enough to satisfy the requirements.


The code above doesn't work, what have I done wrong? @mtf


Ok, so I changed the code a bit and managed to figure it out and pass the lesson. Thanks for your help @mtf

The correct code is:

class Point3D(object):
def init(self, x, y, z):
self.x = x
self.y = y
self.z = z;
def repr(self):
return "(%d, %d, %d)" % (self.x, self.y, self.z)
my_point = Point3D(1, 2, 3)
print my_point

sorry I don't know how to format the code correctly so you will have to figure out the indenting yourself!


@rachel_sugden ,

The names __init__ and __repr__ must begin and end with two underscores to be recognized properly.

Formatting your code when you post it would enable users to see your indentation, those important underscores, and other details.

See How do I format code in my posts?.


The below is my code which works.
Please take care of the indentation.

class Point3D(object):
def init(self,x,y,z):
self.x = x
self.y = y
self.z = z

def __repr__(self):
    return "(%d, %d, %d)" %(self.x,self.y,self.z)

my_point = Point3D(1,2,3)
print my_point


look well sintax init in class must have init ect!!!!