# Add int and Class objects

Asked by At

I'm trying to add a int and class together like 1 + Currency(1) I get the error: TypeError: unsupported operand type(s) for +: 'int' and 'Currency'

If I do Currency(1) + 1 it works correctly.

Is there some way to convert the int into a class object or do I need to convert the class to an int object?

``````class Currency:
"A general currency class"
def __init__(self, kr=0, ore=0):
"Input kr and ore, with default values 0"
self.ore = round(ore + (kr * 100), 0)
self.kr = int(self.ore / 100)
kr_ore = kr + ore / 100

def __add__(self, other):
self, other = Currency.__check_type(self, other)
print(type(self), type(other))

total = self.ore + other.ore
self.kr = int(total / 100)
self.ore = total - self.kr * 100

return Currency(self.kr, self.ore)

def __check_type(self, other):
if type(self) != Currency and type(int) != Currency:
self = Currency(self)
other = Currency(other)

elif type(self) != Currency:
print("RUNNING")
self = Currency(self)

elif type(other) != Currency:
other = Currency(other)
return self, other

__repr__ = __str__

x = 100
y = Currency(1)
print(x+y)
``````

## 1 Answers On Best Solutions

You almost did it, if you printed `y+x` it would work for your code.

`__add__` is called for objects on the left side of the `+` operator, for right side, the dunder method is called `__radd__`.

``````class Currency:
"A general currency class"

def __init__(self, kr=0, ore=0):
"Input kr and ore, with default values 0"

self.ore = round(ore + (kr * 100), 0)
self.kr = int(self.ore / 100)
kr_ore = kr + ore / 100

def __add__(self, other):
self, other = Currency.__check_type(self, other)
print(type(self), type(other))
total = self.ore + other.ore
self.kr = int(total / 100)
self.ore = total - self.kr * 100

def __radd__(self, other):
self, other = Currency.__check_type(self, other)
print(type(self), type(other))
total = self.ore + other.ore
self.kr = int(total / 100)
self.ore = total - self.kr * 100
return Currency(self.kr, self.ore)

def __check_type(self, other):
if type(self) != Currency and type(int) != Currency:
self = Currency(self)
other = Currency(other)
elif type(self) != Currency:
print("RUNNING")
self = Currency(self)
elif type(other) != Currency:
other = Currency(other)
return self, other

x = 100
y = Currency(1)
print(x+y)
``````