AttributeError:“ int”对象没有属性“ display”

时间:2019-12-02 00:55:50

标签: python arrays list loops class

我正在尝试编写一个程序,该程序将数字随机分配给三个不同的列表,然后在不同的类的帮助下,调用函数“ display()”,并显示各种结果。我有我的类和循环,并且随机化降低了,但是每次我尝试打印display()结果时,我都会收到一条错误消息,说没有属性“ display”。我尝试过更改变量名,看是否有帮助,但错误仍然存​​在。

我的代码去了

    class Circle():
    def __init__(self, radius=1):
        self.__radius = radius

    def find_area(self):
        a = self.__radius * self.__radius * 3.142
        return(a)

    def setRadius(self, rad):
        self.__radius = rad

    def getRadius(self):
        return(self.__radius)

    def display(self):
        print("\nClass = Circle")
        print("The area for this circle is:\t")
        return(self.find_area())

class Square():
    def __init__(self, sside=2.3):
        self.__sside = sside

    def find_area(self):
        a = self.__sside * self.__sside 
        return(a)

    def setSSide(self, lw):
        self.__sside = lw

    def getSSide(self):
        return(self.__sside)

    def display(self):
        print("\nClass = Square")
        print("The area for this square is:\t")
        return(self.find_area())

class Cube():
    def __init__(self, cside=2):
        self.__cside = cside

    def find_surfacearea(self):
        sa = 6 * self.__cside * self.__cside
        return(sa)

    def setCSide(self, lwh):
        self.__cside = lwh

    def getCSide(self):
        return(self.__cside)

    def display(self):
        print("\nClass = Cube")
        print("The area for this cube is:\t")
        return(self.find_surfacearea())

x1 = Circle()
print(x1.display())

x2 = Square()
print(x2.display())

x3 = Cube()
print(x3.display())

#Lists and Loops
circlelist = []
squarelist = []
cubelist = []

circlecount = 0
squarecount = 0
cubecount = 0

for i in range (10):
    shape = random.randint(1, 3)
    number = random.randint(0, 10)

    if shape == 1:
        print("\n*Circle*")
        circlecount += 1
        circlelist.append(number)

    elif shape == 2:
        print("\n*Square*")
        squarecount += 1
        squarelist.append(number)

    else:
        print("\n*Cube*")
        cubecount += 1
        cubelist.append(number)

"""x = len(circlelist)
y = len(squarelist)
z = len(cubelist)

for i in range (x):
    circlelist[i].display()
    #print()

for i in range (y):
    squarelist[i].display()

for i in range (z):
    cubelist[i].display()"""

print("\nThe items in the circle list are:", circlelist)
print(len(circlelist))
print("The items in the square list are:", squarelist)
print(len(squarelist))
print("The items in the cube list are:", cubelist)
print(len(cubelist))

for i in range (len(circlelist)):
    circlelist[i].display()

"""for i in range (10):
    circlelist.display()
    squarelist.display()
    cubelist.display()"""

有人可以帮我看看我可能出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

当前,您的列表只是数字列表。您不能在数字上运行display()函数,它必须在您的一个对象上运行。这需要相对简单的更改。而不是数字列表,而是对象列表。

在下面的代码中,我进行了更改,而不是在列表中添加数字,而是添加了一个新对象,该对象具有适当的半径,边,边。

for i in range (10):
    shape = random.randint(1, 3)
    number = random.randint(0, 10)

    if shape == 1:
        print("\n*Circle*")
        circlecount += 1
        circlelist.append(Circle(radius=number))

    elif shape == 2:
        print("\n*Square*")
        squarecount += 1
        squarelist.append(Square(sside=number))

    else:
        print("\n*Cube*")
        cubecount += 1
        cubelist.append(Cube(cside=number))

如何编辑__init__函数以在创建时显示:

    def __init__(self, radius=1):
        self.__radius = radius
        self.display()