Tôi mới vào Vectors và tạo lớp. Tôi cố gắng để xây dựng lớp vector riêng của tôi, nhưng khi tôi vượt qua nó thông qua mã của tôi là:dòng 60, trong bộ trả về make_tuple (l) TypeError: iter() trả về không lặp của kiểu 'Vector'
vị trí + = tiêu đề * distance_moved
nơi vị trí và tiêu đề là cả hai vectơ. tiêu đề được chuẩn hóa. mục tiêu của tôi là lặp lại mã của tôi cho đến vị trí = đích. Điều gì sai với lớp học này?
nhập toán
class Vector(object):
#defaults are set at 0.0 for x and y
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
#allows us to return a string for print
def __str__(self):
return "(%s, %s)"%(self.x, self.y)
# from_points generates a vector between 2 pairs of (x,y) coordinates
@classmethod
def from_points(cls, P1, P2):
return cls(P2[0] - P1[0], P2[1] - P1[1])
#calculate magnitude(distance of the line from points a to points b
def get_magnitude(self):
return math.sqrt(self.x**2+self.y**2)
#normalizes the vector (divides it by a magnitude and finds the direction)
def normalize(self):
magnitude = self.get_magnitude()
self.x/= magnitude
self.y/= magnitude
#adds two vectors and returns the results(a new line from start of line ab to end of line bc)
def __add__(self, rhs):
return Vector(self.x +rhs.x, self.y+rhs.y)
#subtracts two vectors
def __sub__(self, rhs):
return Vector(self.x - rhs.x, self.y-rhs.y)
#negates or returns a vector back in the opposite direction
def __neg__(self):
return Vector(-self.x, -self.y)
#multiply the vector (scales its size) multiplying by negative reverses the direction
def __mul__(self, scalar):
return Vector(self.x*scalar, self.y*scalar)
#divides the vector (scales its size down)
def __div__(self, scalar):
return Vector(self.x/scalar, self.y/scalar)
#iterator
def __iter__(self):
return self
#next
def next(self):
self.current += 1
return self.current - 1
#turns a list into a tuple
def make_tuple(l):
return tuple(l)
Cảm ơn, điều này đã làm cho tôi. Bây giờ nếu chỉ các tài liệu không quá kinh khủng và không đầy đủ, tất cả chúng ta đều có thể tránh được điều này. – Basic