简体   繁体   中英

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

I don't really understand what this error means. everything up to the final line of code works perfectly.

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)

the output to the code should be:

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

But the output i get is:

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'

like i said i really don't understand what this error means and when i look it up it kind of confuses me because the code is not as simple as mine. i have somewhat of an idea what it means but if someone can help explain it to me that would be awesome. Im pretty sure it has to do with the last three lines and how p4 is not in the Point class but like i said im not sure. Thank you for your time.

The error message you're seeing means that you're expecting the p4 object to have the .dist method but the p4 object is a string object. I think what is confusing you is that p1, p2, and p3 are Point objects (so they have the .dist method) but when you execute the .midpt method to intantiate the p4 variable, the method is returning a string. It returns "(1.5,2.0)" vs Point(1.5,2.0). So if you want your code to work you should change the calc variable in the .midpt method to something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM