3

I am trying to make a script in which I am supposed to make a class Dot, which takes a X position, a Y position, and Color. I have made the class and all of the methods to go with it. The problem I am running into is how to apply the method. Here is what I have done:

class Dot:
    '''A class that stores information about a dot on a flat grid
       attributes: X (int) Y (int), C (str)'''

    def __init__(self, xposition, yposition, color):
        '''xposition represents the x cooridinate, yposition represents
           y coordinate and color represent the color of the dot'''
        self.X = xposition
        self.Y = yposition
        self.C = color


    def __str__(self):
        "Creates a string for appropiate display to represent a point"
        return str(self.X) + " " + str(self.Y) + " " + str(self.C)

    def move_up(self,number):
        '''Takes an integer and modifies the point by adding the given
           number to the x-coordinate
           attributes: number (int)'''

        self.Y = number + self.Y

    def move_right(self,number):
        '''Takes an integer and modifies the Point by adding the given number to the y-coordinate
           attributes: number (int)'''

        self.X = number + self.X

    def distance_to(point2):
        '''Takes another Dot and returns the distance to that second Dot
           attributes: X (int) Y (int)'''

        distance = ((self.X - point2.X )**2) + ((self.Y - point2.Y)**2)
        real_distance = distance.sqrt(2)

        return real_distance



point1 = (2,3,"red")
print("Creates a" + " " + str(point1[2]) + " " + "dot with coordinates (" + str(point1[0]) + "," + str(point1[1]) + ")")
point2 = (1,2,"blue")
print("Creates a" + " " + str(point2[2]) + " " + "dot with coordinates (" + str(point2[0]) + "," + str(point2[1]) + ")")
new_point = point1.move_up(3)
print("Moves point up three on the y axis")

Here is what is returned:

AttributeError: 'tuple' object has no attribute 'move_up'
2
  • we're in the same class i think and i got the same problem that you had... did their answers fix the problem? because they didnt fix mine Commented Apr 1, 2015 at 23:46
  • @JaredBanton Why don't you ask your own question then? Commented Apr 2, 2015 at 11:13

2 Answers 2

3

You never instantiate a Dot object, you create a tuple with 3 elements. Change it to:

point1 = Dot(2,3,"red")
point2 = Dot(1,2,"blue")

and instead of

print("Creates a" + " " + str(point1[2]) + " " + "dot with coordinates (" + str(point1[0]) + "," + str(point1[1]) + ")")

use

print "Creates a" + " " + point1.C + " " + "dot with coordinates (" + str(point1.X) + "," + str(point1.Y) + ")"

By the way, the .format() syntax is much more clear:

print "Creates a {0} dot with coordinates ({1}, {2})".format(point1.C, point1.X, point1.Y)
Sign up to request clarification or add additional context in comments.

1 Comment

When I change the script to yours, I receive a type error 'Dot' object does not support indexing. Any idea why this is occurring?
0

Your code:

point1 = (2,3,"red")

does not create an instance of your Dot class - it just creates a tuple.

To create a Dot, you have to call its constructor (__init__), i.e:

point1 = Dot(2,3,"red")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.