11/11 Error


#1
class Point3D(object):
    def __init__(self,x,y,z):
        x = self.x
        y = self.y
        z = self.z
    def __repr__(self,x,y,z):
        return "(%d, %d, %d)" % (str(self.x), str(self.y), str(self.z))
my_point = Point3D(1 ,2 ,3)

Here is my code.I don't see anything wrong in this one but i keep getting this error:
Oops, try again. It looks like you haven't created a Point3D object named my_point.
Can you help me?


#2

Hi @homefork ,

When __init__ is first called, self.x, self.y, and self.z, have not yet been defined. So, this will not work ...

x = self.x
y = self.y
z = self.z

Instead, you must use the values of the parameters, x, y, z to assign values to self.x, self.y, and self.z, as follows ...

self.x = x
self.y = y
self.z = z

In the __repr__ method, you have used % as a format specifier, therefore, you should not use the str function to convert the numbers to strings.

At the bottom of your code, output the value of my_point, as follows ...

print my_point

So, you should be doing this ...

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

#3

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


#4

I do this like what you do but it tell me that i didn't print "(1,2,3)", I print (1,2,3)