This code is not running ,why?

class array:
def init(self,length):
self.maxlength = length
self.list = []
self.size = 0

def add_elements(self,value):
    if self.size<self.maxlength:
        if self.size!= 0:
            if type(value)==type(self.list[0]):
                self.list.append(value)
                self.size+=1
            else:
                print("data type not matching")
        else:
            self.list.append(value)
            self.size+=1
            
    else:
        print("Array is full")

arr = array(5)
arr.add_elements()

1 Like

@praveenkumar01092001

Please paste the error screenshot.
So that I can check accordingly and clear your doubts

@praveenkumar01092001
Please share your errors here so that we can help.

@praveenkumar01092001 Your code has some syntax error.I have rectified it.You can check it now whether it is working or not.

class Array:
    def __init__(self, length):
        self.maxlength = length
        self.list = []
        self.size = 0

    def add_elements(self, value):
        if self.size < self.maxlength:
            if self.size != 0:
                if type(value) == type(self.list[0]):
                    self.list.append(value)
                    self.size += 1
                else:
                    print("data type not matching")
            else:
                self.list.append(value)
                self.size += 1

        else:
            print("Array is full")

arr = Array(1)
arr.add_elements(10)
arr.add_elements(10)
1 Like