如何在 class 中设置用户输入的值并在第二个 class 中使用它们?

How can I set values from user input in a class and use them in a second class?

我正在编写一个有 2 个选项的 python 代码。根据用户输入计算距离和面积,或根据默认点值进行计算。 对于默认点值,我让代码正常工作。尽管如此,当用户输入积分时,我无法让它工作。我目前可以很好地阅读输入,但我无法在第 2 和第 3 次使用它们 class(折线和多边形)

import math

#Point class
class Point():
    def __init__(self):
        self.x = 0
        self.y = 0
    def setXY(self,x,y):
        self.x = x
        self.y = y

#Class instantiation
p1 = Point()
p2 = Point()
p3 = Point()

val = 0

print("\n----Please select from the following options:----")
print("Enter 1 to set your point coordinates")
print("Enter 2 to use default point coordinates\n")

val = int(input("Enter your value: "))
if val == 1:
    #Set point values for P1
    p1_x = int(input("Enter x value for P1: "))
    p1_y = int(input("Enter y value for P1: "))
    #Set point values for P2
    p2_x = int(input("Enter x value for P2: "))
    p2_y = int(input("Enter y value for P2: "))
    #Set point values for P3
    p3_x = int(input("Enter x value for P3: "))
    p3_y = int(input("Enter y value for P3: "))
    #Set point values
    p1.setXY(p1_x,p1_y)
    p2.setXY(p2_x,p2_y)
    p3.setXY(p3_x,p3_y)
    #Print points values
    print("Location of point P1 is: (",p1.x,",",p1.y,")")
    print("Location of point P2 is: (",p2.x,",",p2.y,")")
    print("Location of point P3 is: (",p3.x,",",p3.y,")")
    print("\n")

    #Polyline class
    class Polyline():
        def __init__(self):
            self.x = 0
            self.y = 0
        def setXY(self,x,y):
            self.x = x
            self.y = y

    #getLength() method for distance calculation
    #Distance formula: root square of[(x2-x1)^2 + (y2-y1)^2]
        def getLength(self, p):
            return math.sqrt((self.x-p.x)**2+(self.y-p.y)**2)

    #Class instantiation
    p1 = Polyline()
    p2 = Polyline()
    p3 = Polyline()
    #Set point values
    print("Location of point P1 is: (",p1.x,",",p1.y,")")
    p1.setXY(p1.x,p1.y)
    p2.setXY(p2.x,p2.y)
    p3.setXY(p3.x,p3.y)
    #Creating variables and setting their value to their respective distance
    resultPoint1_2 = p1.getLength(p2)
    resultPoint2_3 = p2.getLength(p3)
    resultPoint1_3 = p1.getLength(p3)
    #Print distance values
    print("Length of the perimeter from P1 to P2 is: ", resultPoint1_2)
    print("Length of the perimeter from P2 to P3 is: ", resultPoint2_3)
    print("Length of the perimeter from P1 to P3 is: ", resultPoint1_3)
    print("\n")

    #Polygon class
    class Polygon():
        def __init__(self):
            self.x = 0
            self.y = 0
        def setXY(self,x,y):
            self.x = x
            self.y = y

    #getLength() method for distance calculation
    #Distance formula: root square of[(x2-x1)^2 + (y2-y1)^2]
        def getLength(self, p):
            return math.sqrt((self.x-p.x)**2+(self.y-p.y)**2)

    #Class instantiation
    p1 = Polygon()
    p2 = Polygon()
    p3 = Polygon()
    #Set point values
    p1.setXY(p1_x,p1_y)
    p2.setXY(p2_x,p2_y)
    p3.setXY(p3_x,p3_y)
    #Creating variables and setting their value to their respective distance
    resultPoint1_2 = p1.getLength(p2)
    resultPoint2_3 = p2.getLength(p3)
    resultPoint1_3 = p1.getLength(p3)
    #Creating perimeter variable and setting its value according to the formula
    #Perimeter formula for 3 points polygon: length1 + length2 + length3
    perimeter = resultPoint1_2 + resultPoint2_3 + resultPoint1_3
    #Print perimeter value
    print("Perimeter of the polygon is: ", perimeter)
    print("\n")

所有点的 x 和 y 值输入 3 时的当前输出:

Location of point P1 is: ( 3 , 3 )
Location of point P2 is: ( 3 , 3 )
Location of point P3 is: ( 3 , 3 )


Location of point P1 is: ( 0 , 0 )
Length of the perimeter from P1 to P2 is:  0.0
Length of the perimeter from P2 to P3 is:  0.0
Length of the perimeter from P1 to P3 is:  0.0


Perimeter of the polygon is:  0.0

如您所见,第二个和第三个 class 没有使用第一个 class 上设置的值。

您需要为点、多段线和多边形使用不同的变量。否则,当你写类似

p1.setXY(p1.x, p1.y)

这是从您尝试更新的多边形获取 p1.xp1.y,而不是您之前创建的点。

在下面的代码中,p1, p2, p3 是点,pl1, pl2, pl3 是折线,pg1, pg2, pg3 是多边形。

import math

#Point class
class Point():
    def __init__(self):
        self.x = 0
        self.y = 0
    def setXY(self,x,y):
        self.x = x
        self.y = y

#Class instantiation
p1 = Point()
p2 = Point()
p3 = Point()

val = 0

print("\n----Please select from the following options:----")
print("Enter 1 to set your point coordinates")
print("Enter 2 to use default point coordinates\n")

val = int(input("Enter your value: "))
if val == 1:
    #Set point values for P1
    p1_x = int(input("Enter x value for P1: "))
    p1_y = int(input("Enter y value for P1: "))
    #Set point values for P2
    p2_x = int(input("Enter x value for P2: "))
    p2_y = int(input("Enter y value for P2: "))
    #Set point values for P3
    p3_x = int(input("Enter x value for P3: "))
    p3_y = int(input("Enter y value for P3: "))
    #Set point values
    p1.setXY(p1_x,p1_y)
    p2.setXY(p2_x,p2_y)
    p3.setXY(p3_x,p3_y)
    #Print points values
    print("Location of point P1 is: (",p1.x,",",p1.y,")")
    print("Location of point P2 is: (",p2.x,",",p2.y,")")
    print("Location of point P3 is: (",p3.x,",",p3.y,")")
    print("\n")

    #Polyline class
    class Polyline():
        def __init__(self):
            self.x = 0
            self.y = 0
        def setXY(self,x,y):
            self.x = x
            self.y = y

    #getLength() method for distance calculation
    #Distance formula: root square of[(x2-x1)^2 + (y2-y1)^2]
        def getLength(self, p):
            return math.sqrt((self.x-p.x)**2+(self.y-p.y)**2)

    #Class instantiation
    pl1 = Polyline()
    pl2 = Polyline()
    pl3 = Polyline()
    #Set point values
    print("Location of point P1 is: (",p1.x,",",p1.y,")")
    pl1.setXY(p1.x,p1.y)
    pl2.setXY(p2.x,p2.y)
    pl3.setXY(p3.x,p3.y)
    #Creating variables and setting their value to their respective distance
    resultPoint1_2 = pl1.getLength(p2)
    resultPoint2_3 = pl2.getLength(p3)
    resultPoint1_3 = pl1.getLength(p3)
    #Print distance values
    print("Length of the perimeter from P1 to P2 is: ", resultPoint1_2)
    print("Length of the perimeter from P2 to P3 is: ", resultPoint2_3)
    print("Length of the perimeter from P1 to P3 is: ", resultPoint1_3)
    print("\n")

    #Polygon class
    class Polygon():
        def __init__(self):
            self.x = 0
            self.y = 0
        def setXY(self,x,y):
            self.x = x
            self.y = y

    #getLength() method for distance calculation
    #Distance formula: root square of[(x2-x1)^2 + (y2-y1)^2]
        def getLength(self, p):
            return math.sqrt((self.x-p.x)**2+(self.y-p.y)**2)

    #Class instantiation
    pg1 = Polygon()
    pg2 = Polygon()
    pg3 = Polygon()
    #Set point values
    pg1.setXY(p1_x,p1_y)
    pg2.setXY(p2_x,p2_y)
    pg3.setXY(p3_x,p3_y)
    #Creating variables and setting their value to their respective distance
    resultPoint1_2 = pg1.getLength(p2)
    resultPoint2_3 = pg2.getLength(p3)
    resultPoint1_3 = pg1.getLength(p3)
    #Creating perimeter variable and setting its value according to the formula
    #Perimeter formula for 3 points polygon: length1 + length2 + length3
    perimeter = resultPoint1_2 + resultPoint2_3 + resultPoint1_3
    #Print perimeter value
    print("Perimeter of the polygon is: ", perimeter)
    print("\n")