Problem with accessing array using loop [python]

Hi,
I have written a simple program to access array in Python. I am getting a red line after array. My code is:

class TestSort:
    arr= [4, 9, 12, 3, 5, 24]
    def __init__(self, n):
        self.n=n
    def sorting( ):
        for i in arr:

problem with accessing arr

1 Like

You should use class name to access variables defined as class variables. i.e. ClassName.variable
Additional thing you are likely to encounter soon - still looking at the sorting method - remember that every method requires at least one parameter.

1 Like

Its not printing anything and no error:

class TestSort:
    arr = [4, 9, 12, 3, 5, 24]  # class var access by className.arr

    def __init__(self, n):
        self.n = n

    def sorting(self):
        for i in TestSort.arr:
            for j in TestSort.arr:
                if (TestSort.arr[j] > TestSort.arr[j + 1]):
                    temp = TestSort.arr[j]
                    TestSort.arr[j] = TestSort.arr[j + 1]
                    TestSort.arr[j + 1] = temp

    def main():
        for i in TestSort.arr:
            print(TestSort.arr[i])


if __name__ == "__main__":
    main()

Somebody please guide me.

Zulfi.

Your main() function is not defined outside of the class, so you need to call it by referencing class as well.

In python for i in some_list don’t iterate by list indices, but by elements within that list.
To iterate by index you need to create list starting from 0 up to length of the list, this can be achieved using range() and len() functions.
Or even better use enumerate() on list in the for loop, that way you can get both list index and the value of certain list element.

Hi,
Thanks for your response. I changed the code to:

lass TestSort:
    arr = [4, 9, 12, 3, 5, 24]  # class var access by className.arr

    def __init__(self, n):
        self.n = n

    def sorting(self):
        for i in TestSort.arr:
            for j in TestSort.arr:
                if (TestSort.arr[j] > TestSort.arr[j + 1]):
                    temp = TestSort.arr[j]
                    TestSort.arr[j] = TestSort.arr[j + 1]
                    TestSort.arr[j + 1] = temp

    def main():
        for i in enumerate(TestSort.arr):
            print(TestSort.arr[i])


if __name__ == "__main__":
    objTestSort = TestSort(6)
    objTestSort.main()

I am getting following error message:

/bubblesort.py
Traceback (most recent call last):
File “/bubblesort.py”, line 22, in
objTestSort.main()
TypeError: main() takes 0 positional arguments but 1 was given

Process finished with exit code 1

This is about what I was writing before. Every method inside class have to have at least one parameter.

Hi,
Thanks for your help but still I am getting the errors:

class TestSort:
    arr = [4, 9, 12, 3, 5, 24]  # class var access by className.arr

    def __init__(self, n):
        self.n = n

    def sorting(self):
        for i in TestSort.arr:
            for j in TestSort.arr:
                if (TestSort.arr[j] > TestSort.arr[j + 1]):
                    temp = TestSort.arr[j]
                    TestSort.arr[j] = TestSort.arr[j + 1]
                    TestSort.arr[j + 1] = temp

    def main(self):
        for i in enumerate(TestSort.arr):
            print(TestSort.arr[i])


if __name__ == "__main__":
    objTestSort = TestSort(6)
    objTestSort.main(objTestSort)

/home/zulfi/PycharmProjects/sort/venv/bin/python /home/zulfi/PycharmProjects/sort/bubblesort.py
Traceback (most recent call last):
File “/home/zulfi/PycharmProjects/sort/bubblesort.py”, line 22, in
objTestSort.main(objTestSort)
TypeError: main() takes 1 positional argument but 2 were given

Hi,
My output is not correct:

class TestSort:
    arr = [4, 9, 12, 3, 5, 24]  # class var access by className.arr

    def __init__(self, n):
        self.n = n

    def sorting(self):
        for i in TestSort.arr:
            for j in TestSort.arr:
                if (TestSort.arr[j] > TestSort.arr[j + 1]):
                    temp = TestSort.arr[j]
                    TestSort.arr[j] = TestSort.arr[j + 1]
                    TestSort.arr[j + 1] = temp

    def main(self):
        for i, v in enumerate(TestSort.arr):
            print(TestSort.arr[i])


if __name__ == "__main__":
    objTestSort = TestSort(6)
    objTestSort.main()

My output is same as my input list. There is no sorting.

Zulfi.

Take a look at your class. Which method is supposed to do the sorting? Is it being called somewhere?

Hi,
Thanks for your help. I am calling sorting but I am getting errors:

class TestSort:
    arr = [4, 9, 12, 3, 5, 24]  # class var access by className.arr

    def __init__(self, n):
        self.n = n

    def sorting(self):
        for i in TestSort.arr:
            for j in range(TestSort.arr)-1:
                if (TestSort.arr[j] > TestSort.arr[j + 1]):
                    temp = TestSort.arr[j]
                    TestSort.arr[j] = TestSort.arr[j + 1]
                    TestSort.arr[j + 1] = temp

    def main(self):
        self.sorting()
        for i, v in enumerate(TestSort.arr):
            print(TestSort.arr[i])


if __name__ == "__main__":
    objTestSort = TestSort(6)
    objTestSort.main()

I am getting following errors:

/PycharmProjects/sort/bubblesort.py
Traceback (most recent call last):
File “/PycharmProjects/sort/bubblesort.py”, line 23, in
objTestSort.main()
File “/PycharmProjects/sort/bubblesort.py”, line 16, in main
self.sorting()
File “/PycharmProjects/sort/bubblesort.py”, line 9, in sorting
for j in range(TestSort.arr)-1:
TypeError: ‘list’ object cannot be interpreted as an integer

Process finished with exit code 1
Somebody please guide me.
Zulfi.