Whats wrong with my code? 11.building useful classes


What should i change in my code? Do those quotes before parentheses causes problem?


Oops, try again. It looks like you didn't print "(1, 2, 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


Hi, @sadesoda ,

Check your output very closely. You printed ...


... but should have printed ...

(1, 2, 3)


sorry, it doesnt work too


@sadesoda ,

The quotes before the parentheses are not the problem. In fact, they are needed in order to delimit the format string.

The problem is that your format string does not include a space after each of the two commas. You have ...

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

This part of the statement is the format string, and is the only thing you need to change ...


Without spaces after the commas, the __repr__ method returned ...


... instead of ...

(1, 2, 3)

If you put a space after each of the commas in the format string, it will produce the output format specified in the instructions.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.