简体   繁体   中英

AttributeError: 'int' object has no attribute 'display'

I am attempting to write a program that randomly assigns numbers to three different lists and then, with the help of different classes, calls the function "display()", and displays various results. I've got my classes and the loop with the randomization down, but every time I try to print display() results, I get an error saying there is no attribute 'display'. I've tried changing variable names to see if that would help but the error persists.

My code goes:

    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()"""

Could someone help me see where I might be going wrong?

Currently, your lists are simply lists of numbers. You can't run the display() function on numbers, it has to be run on one of your objects. This requires a relatively simple change. Rather than have a list of numbers, have a list of objects.

In the code below, I changed it so rather than appending a number to the list, it appends a new object with the random number as the radius,sside,cside as appropriate.

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

How to edit __init__ functions to display upon creation:

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

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