如何解决此属性错误。我想找出一个点到原点的距离

问题描述 投票:0回答:1
AttributeError                            Traceback (most recent call last)
<ipython-input-34-1cdc6079623b> in <cell line: 1>()
----> 1 p1.distance_from_origin()

1 frames
<ipython-input-31-80a364aac50f> in distance_of_points(self, other)
      4     self.y = y
      5   def distance_of_points(self,other):
----> 6     d = ((self.x-other.x)**2+(self.y-other.y)**2)**0.5
      7     return d
      8   def distance_from_origin(self):

AttributeError: 'tuple' object has no attribute 'x'

class Points:
  def __init__(self,x,y):
    self.x = x
    self.y = y
  def distance_of_points(self,other):
    d = ((self.x-other.x)**2+(self.y-other.y)**2)**0.5
    return d
  def distance_from_origin(self):
    return self.distance_of_points((0,0))

p1 = Points(3,4)
p1.distance_from_origin()
python
1个回答
0
投票

使 (0, 0) 元组成为一个点:

return self.distance_of_points(Point(0,0))
© www.soinside.com 2019 - 2024. All rights reserved.