AttributeError: 'str' object has no attribute 'dist'

AttributeError: 'str' object has no attribute 'dist'

我不太明白这个错误是什么意思。直到最后一行代码的一切都完美无缺。

import math 
# the 2D point class
class Point(object):

    def __init__(self, x = 0, y = 0):
        self.x = x
        self.y = y
    #accessor for x
    @property
    def x(self):
        return self._x

    #mutator for x
    @x.setter
    def x(self, value):
        if (value != 0):
            self._x = float(value)
        else:
            self._x = float(0.0)
    #accessor for y
    @property
    def y(self):
        return self._y

    #mutator for y
    @y.setter
    def y(self, value):
        if (value != 0):
            self._y = float(value)
        else:
            self._y = float(0.0)
    def __str__(self):
        return "{},{}".format(self.x, self.y)

    #This finds the distance between 2 points
    def dist(x, y):
        x1 = x.x
        x2 = y.x
        y1 = x.y
        y2 = y.y

        calc = "{}".format(math.sqrt((x2-x1)**2 + (y2-y1)**2))
        return calc

    #This finds the midpoint of 2 points
    def midpt(x,y):
        x1 = x.x
        x2 = y.x
        y1 = x.y
        y2 = y.y

        calc = "({},{})".format(((x1+x2)/2),((y1+y2)/2))
        return calc

##########################################################
# ***DO NOT MODIFY OR REMOVE ANYTHING BELOW THIS POINT!***
# create some points
p1 = Point()
p2 = Point(3, 0)
p3 = Point(3, 4)
# display them
print "p1:", p1
print "p2:", p2
print "p3:", p3
# calculate and display some distances
print "distance from p1 to p2:", p1.dist(p2)
print "distance from p2 to p3:", p2.dist(p3)
print "distance from p1 to p3:", p1.dist(p3)
# calculate and display some midpoints
print "midpt of p1 and p2:", p1.midpt(p2)
print "midpt of p2 and p3:", p2.midpt(p3)
print "midpt of p1 and p3:", p1.midpt(p3)
# just a few more things...
p4 = p1.midpt(p3)
print "p4:", p4
print "distance from p4 to p1:", p4.dist(p1)

代码的输出应该是:

p1: 0.0,0.0
p2: 3.0,0.0
p3: 3.0,4.0
distance from p1 to p2: 3.0
distance from p2 to p3: 4.0
distance from p1 to p3: 5.0
midpt of p1 and p2: (1.5,0.0)
midpt of p2 and p3: (3.0,2.0)
midpt of p1 and p3: (1.5,2.0)
p4: (1.5,2.0)
distance from p4 to p1: 2.5

但我得到的输出是:

p1: 0.0,0.0
p2: 3.0,0.0
p3: 3.0,4.0
distance from p1 to p2: 3.0
distance from p2 to p3: 4.0
distance from p1 to p3: 5.0
midpt of p1 and p2: (1.5,0.0)
midpt of p2 and p3: (3.0,2.0)
midpt of p1 and p3: (1.5,2.0)
p4: (1.5,2.0)
distance from p4 to p1:

Traceback (most recent call last):
  File "C:\Users\owens\Downloads 2D Points-TEMPLATE.py", line 81, in <module>
    print "distance from p4 to p1:", p4.dist(p1)
AttributeError: 'str' object has no attribute 'dist'

就像我说的,我真的不明白这个错误是什么意思,当我查找它时,我有点困惑,因为代码不像我的那么简单。我有点明白它的意思,但如果有人能帮我解释一下,那就太棒了。我很确定它与最后三行以及 p4 如何不在点 class 中有关,但就像我说的那样我不确定。谢谢你的时间。

您看到的错误消息意味着您希望 p4 对象具有 .dist 方法,但 p4 对象是一个字符串对象。我认为让您感到困惑的是 p1、p2 和 p3 是 Point 对象(因此它们具有 .dist 方法)但是当您执行 .midpt 方法来实例化 p4 变量时,该方法返回一个字符串。它 returns "(1.5,2.0)" 与 Point(1.5,2.0)。因此,如果您希望您的代码正常工作,您应该将 .midpt 方法中的 calc 变量更改为如下内容:

calc = 点((x1+x2)/2), ((y1+y2)/2))