AttributeError:'str'对象没有属性'dist'

问题描述 投票:-2回答:1

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

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类中,但就像我说我不确定。感谢您的时间。

python python-2.7 point
1个回答
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))

© www.soinside.com 2019 - 2024. All rights reserved.