用Python填充三角形

时间:2018-03-25 11:31:42

标签: python function math fill

我有一个python程序,其中对象是让用户输入三角形边长,如果他们想填充它,以及什么颜色。我一直得到一个与GeometricObject和fill有关的属性错误。

经过一些修复后,我再次陷入同一区域(填充),这是我不断得到的错误:

> Traceback (most recent call last):   File "Desktop/one.py", line 81,
> in <module>
>     main()   File "Desktop/one.py", line 78, in main
>     "Is filled? :  ", triangle.ColorFill())   File "Desktop/one.py", line 21, in ColorFill
>     return self.__filled AttributeError: 'Triangle' object has no attribute '_GeometricObject__filled'

我不知道在哪里解开这个问题,欢迎任何帮助。以下是我的代码:

12.1三角类

import math

#class for the GeometricObject, must be defined before Trianble. includes fill
class GeometricObject:
    #if true the color will be green
    def __init__(self, color =" ", filled = True):
        self.__color = color
        self__filled = filled
    #getting the fill color and setting it to green fill
    def getColor(self):
        return self.__color
    def setColor(self, color):
        self.__color = color
    def ColorFill(self):
        return self.__filled
    def setColorFill(self, filled):
        self__filled = filled
    #string that tells you if it is filled or not
    def __st__(self):
        return "Color is: " + (self.__color) \
               + "Fill: " + str(self.__filled)

#setting class for triangle and calling on geometric objects
class Triangle(GeometricObject):
    def __init__(self, side1 = 1.0, side2 = 1.0, side3 = 1.0):
        self.__side1 = side1
        self.__side2 = side2
        self.__side3 = side3
        #calling GeometricObject, this is what triangle is bound to
        GeometricObject.__init__(self)
     #getting length of sides       
    def getLengthA(self):
        return side1
    def getLengthB(self):
        return side2
    def getLengthC(self):
        return side3
    #setting and getting perimeter
    def setPerimeter(self):
        self.perimeter = perimeter
    def getPerimeter(self):
        perimeter = self.__side1 + self.__side2 + self.__side3
        return perimeter
    #setting and getting area
    def setArea(self):
        self.area = area
    def getArea(self):
        s = (self.__side1 + self.__side2 + self.__side3) / 2
        area = math.sqrt(s * ((s - self.__side1) * (s - self.__side2) * (s - self.__side3)))
        return area
    #printing string, sides
    def toString(self):
        return "Triangle: Side 1: " + (self.__side1)\
               + "Side 2: " + (self.__side2)\
               + "Side 3: " + (self.__side3)

def main():
    #user enters sides of a triangle
    side1, side2, side3 = eval(input("Enter the length of the Tiangle's sides (A, B, C):  "))
    triangle = Triangle(side1, side2, side3)
    #user enters color
    color = input("Enter a color: ")
    triangle.setColor(color)
    #user enter if filled
    filled = eval(input("Enter 1 to fill with color \n Enter 0 for no fill: "))
    ColorFill = (filled == 1)
    triangle.setColorFill(ColorFill)

    print("The area of the triangle is: " ,  triangle.getArea(), \
          "The perimeter is: " , triangle.getPerimeter(), \
          "The color is: ", triangle.getColor(), \
          "Is filled? :  ", triangle.ColorFill())


main()

0 个答案:

没有答案