AttributeError:“ str”对象没有属性“ dist”

时间:2019-03-17 19:21:55

标签: python python-2.7 point

我不太了解此错误的含义。直到代码最后一行的所有内容都可以正常工作。

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\01 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如何不在Point类中有关,但是就像我说的我不确定。谢谢您的宝贵时间。

1 个答案:

答案 0 :(得分:0)

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

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