Building Useful Classes - Why is this wrong?


#1

https://www.codecademy.com/en/courses/python-intermediate-en-egNXj/1/5

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


#2

Indentation error.

def repr(self):

should line up with,

def __init__(...):

#3

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


#4

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.


#5

did u mean like this? @mtf


#6

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.


#7

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


#8

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!


#9

@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?.


#10

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


#11

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