Skip to content
Advertisement

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

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'

Advertisement

Answer

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)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement