2

I'm getting this error :

Traceback (most recent call last):
  File "C:/Users/mali03/Desktop/Python/Practice/p2.py", line 18, in <module>
    first.subtraction(1, 2)
TypeError: subtraction() missing 1 required positional argument: 'y'

Here is my calculator class

class calculator:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def addition(self, x, y):
        return self.x + self.y

    def subtraction(self, x, y):
        if self.x > self.y:
            return self.y - self.x
        else:
            return self.x - self.y

I then call subtraction with the following:

first = calculator
first.subtraction(1, 2)
ᴀʀᴍᴀɴ
  • 4,443
  • 8
  • 37
  • 57
mali30
  • 57
  • 1
  • 2
  • 7

4 Answers4

3

Like stated previously, you don't have to include parameters in your addition or subtraction functions if you already gather that information in the __init__ function.

Like so:

class calculator:

def __init__(self, x, y):
    self.x = x
    self.y = y

def addition(self):
    return self.x + self.y

def subtraction(self):
    if self.x > self.y:
        return self.y - self.x
    else:
        return self.x - self.y


first = calculator
print(first(5,10).addition())

Alternatively, if you do want to have x and y parameters in your addition and subtraction functions, you can adjust your code like so:

class calculator:

def addition(self, x, y):
    return x + y

def subtraction(self, x, y):
    if x > y:
        return y - x
    else:
        return x - y

first = calculator
print(first().addition(5, 10))

Where parameters of individual functions are used instead to the parameters given to the class object.

Either ways work, it depends on how you want to use the class.

Austin B
  • 184
  • 1
  • 1
  • 8
2

Alternatively you could do:

class calculator():

    def addition(self, x, y):
        return x + y

    def subtraction(self, x, y):
        if x > y:
            return y - x
        else:
            return x - y

first = calculator()
print(first.subtraction(1, 2)) 

Also not entirely sure if x > y: was your intention or if you really wanted if x < y:

tgikal
  • 1,667
  • 1
  • 13
  • 27
1

You don't need to specify x and y in subtraction or addition:

class calculator:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def addition(self):
        return self.x + self.y

    def subtraction(self):
        if self.x > self.y:
            return self.y - self.x
        else:
            return self.x - self.y

self will cover retrieving x and y for you, since you are referencing those instance variables. Otherwise, you will need to specify them on call:

# Yields 2-1 rather than 4-3
result = calculator(1,2).subtraction(3,4)

You do, however, need to specify them when instantiating your class

myinst = calculator(1,2)
C.Nivs
  • 12,353
  • 2
  • 19
  • 44
0

first = calculator makes first refer to the class. But first = calculator(1, 2) makes first an object of the class calculator. This initializes self for all other functions called on first. This also sets the values for self.x and self.y because __init__ is called as soon as object is created. Hence, when first = calculator(x, y) is used, self has a value along with self.x and self.y, and when parentheses are not used, they do not have a value. That gives a missing argument error.

Next, as others have already referred, once __init__ is called, x and y are initialized for the object. And so, you don't need them while declaring other functions. self.x and self.y can directly be used there which will now refer to 1 and 2 respectively in this case.

Hope it helps.

shraiysh
  • 106
  • 1
  • 8