Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Here is the problem discription: print(snake_array[0].x) AttributeError: 'list' object has no attribute 'x' . I cannot find the mistake

class Test:
    def __init__(self, x, y, dir):
        self.x = x
        self.y = y
        self.dir = dir


def snake_init(snake_array):
    snake_array.append([Test(300, 300, "RIGHT")])
    snake_array.append([Test(301, 300, "RIGHT")])
    snake_array.append([Test(302, 300, "RIGHT")])
    print(snake_array[0].x)


snake_array = []
snake_init(snake_array)

>Solution :

The problem is that you are appending lists to snake_array, not Test objects. This is what you’re appending: [Test(300, 300, "RIGHT")]. Notice that the brackets make it a list. All you need to do is remove the extra brackets. Like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Test:
    def __init__(self, x, y, dir):
        self.x = x
        self.y = y
        self.dir = dir


def snake_init(snake_array):
    snake_array.append(Test(300, 300, "RIGHT"))
    snake_array.append(Test(301, 300, "RIGHT"))
    snake_array.append(Test(302, 300, "RIGHT"))
    print(snake_array[0].x)


snake_array = []
snake_init(snake_array)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading