【发布时间】:2015-04-01 21:03:58
【问题描述】:
我正在尝试制作一个脚本,我应该在其中制作一个 Dot 类,它采用 X 位置、Y 位置和颜色。我已经制作了课程和所有的方法。我遇到的问题是如何应用该方法。这是我所做的:
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")
这是返回的内容:
AttributeError: 'tuple' object has no attribute 'move_up'
【问题讨论】:
-
我想我们在同一个班级,我遇到了和你一样的问题……他们的答案解决了问题吗?因为他们没有修好我的
-
@JaredBanton 那你为什么不问你自己的问题呢?