簡體   English   中英

AttributeError: 'list' object 沒有屬性 'dist'

[英]AttributeError: 'list' object has no attribute 'dist'

我一直在為我的一門入門課程做作業。 我幾乎完成了我的代碼,但我不斷收到“AttributeError:'tuple' object 沒有屬性'dist'。” 我知道問題從 midpt function 開始,我需要返回 class 的實例而不是元組; 但是,我無法做到這一點。 你能看看我的代碼嗎? 將不勝感激。

'''

import math
class Point(object):

    # The contructor for Point class
    def __init__(self, x = 0, y = 0):
        self.x = float(x)
        self.y = float(y)

    # The getter for x
    @property
    def x(self):
        return self._x
    # The setter for x
    @x.setter
    def x(self, value):
        self._x = value

    # The getter for y
    @property
    def y(self):
        return self._y
    # The setter for y
    @y.setter
    def y(self, value):
        self._y = value

    # Function for getting the distance between two points
    def dist(self, other):
        xVar = (other.x - self.x) ** 2
        yVar = (other.y - self.x) ** 2
        equation = xVar + yVar
        distance = math.sqrt(equation)
        return distance

    # Function for getting the midpoint
    def midpt(self, other):
        xVar = (other.x - self.x) / 2
        yVar = (other.y - self.y) / 2
        midpoint = (xVar,yVar)
        return midpoint

    # Magic function for printing
    def __str__(self):
        return "({},{})".format(self.x, self.y)

##########################################################
# ***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))

您可能希望將midpoint = (xVar,yVar)更改為midpoint = Point(xVar, yVar)

這樣, p4是一個Point (而不是元組)實例,您可以在其上調用dist方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM