TypeError: __init__() missing 2 required positional arguments: 'arr' and 'n'

Question:

This is a problem of the missing number in python.

class Missing:

    n = int(input())
    arr = list(map(int,input().split(" "))) 

    def __init__(self,arr,n):
        self.arr = arr
        self.n = n
    
    def MissingNumber(self):
        self.res = self.n*(self.n+1)/2
        self.sum_array = sum(self.arr)
        return "Missing no. is ",self.res-self.sum_array

Obj = Missing()
Obj.MissingNumber()

I am getting this error.
can anybody solve it?

Obj = Missing()
TypeError: __init__() missing 2 required positional arguments: 'arr' 
and 'n'
Asked By: Ajay Kathwate

||

Answers:

your constructor need two parameter. You need to assign it before it run.

you need to assign n and arr outside class object

class Missing:
    def __init__(self,arr,n):
        self.arr = arr
        self.n = n
    
    def MissingNumber(self):
        self.res = self.n*(self.n+1)/2
        self.sum_array = sum(self.arr)
        return "Missing no. is ",self.res-self.sum_array

if __name__ == '__main__':
    n = int(input())
    arr = list(map(int,input().split(" "))) 
    Obj = Missing(n,arr)
    Obj.MissingNumber()
Answered By: AlexMoshi

you need put the input outside class,and assign it when you create instance by Obj = Missing(arr,n)

code:

class Missing:
    def __init__(self,arr,n):
        self.arr = arr
        self.n = n
        
    def MissingNumber(self):
        self.res = self.n*(self.n+1)/2
        self.sum_array = sum(self.arr)
        return "Missing no. is ",self.res-self.sum_array
        
n = int(input())
arr = list(map(int,input().split(" "))) 
Obj = Missing(arr,n) 
print(Obj.MissingNumber())

result:

5
1 2 3 4 5
('Missing no. is ', 0.0)
Answered By: leaf_yakitori
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.