+ 1

im getting a type error and idk how to fix it

class Complex: def __init__(self,real='',imaginary='_'): if(real=='' and imaginary==''): self.real=int(input("Enter real part = ")) self.imaginary=int(input("Enter imaginary part = ")) else: self.real=real self.imaginary=imaginary def _add_(self,other): real=self.real+other.real imaginary=self.imaginary+other.imaginary s3=Complex(real,imaginary) return s3 def _sub_(self,other): real=self.real-other.real imaginary=self.imaginary-other.imaginary s3=Complex(real,imaginary) return s3 def _repr_(self): return("Real part = {}, Imaginary part = {}".format(self.real,self.imaginary)) a=Complex() b=Complex() c=a+b print(a) print(b) print(c)

12th May 2020, 6:11 PM
Yash
Yash - avatar
10 odpowiedzi
0
class Complex: def __init__(self,real='',imaginary=''): if(real=='' and imaginary==''): self.real=int(input("Enter real part = ")) self.imaginary=int(input("Enter imaginary part = ")) else: self.real=real self.imaginary=imaginary def __add__(self,other): real=self.real+other.real imaginary=self.imaginary+other.imaginary s3=Complex(real,imaginary) return s3 def __sub__(self,other): real=self.real-other.real imaginary=self.imaginary-other.imaginary s3=Complex(real,imaginary) return s3 def __repr__(self): return("Real part = {}, Imaginary part = {}".format(self.real,self.imaginary)) a=Complex() b=Complex() c=a+b print(a) print(b) print(c)
12th May 2020, 6:31 PM
Russ
Russ - avatar
+ 1
Try putting double underscores around add and sub (so __add__ and __sub__). Magic Methods require double underscores. Same with repr.
12th May 2020, 6:17 PM
Russ
Russ - avatar
+ 1
i removed the underscores from the add, sub and repr functions. its running but im getting an error on line 26 which is "c=a+b" This is the updated code: class Complex: def __init__(self,real='',imaginary=''): if(real=='' and imaginary==''): self.real=int(input("Enter real part = ")) self.imaginary=int(input("Enter imaginary part = ")) else: self.real=real self.imaginary=imaginary def add(self,other): real=self.real+other.real imaginary=self.imaginary+other.imaginary s3=Complex(real,imaginary) return s3 def sub(self,other): real=self.real-other.real imaginary=self.imaginary-other.imaginary s3=Complex(real,imaginary) return s3 def repr(self): return("Real part = {}, Imaginary part = {}".format(self.real,self.imaginary)) a=Complex() b=Complex() c=a+b print(a) print(b) print(c)
12th May 2020, 6:28 PM
Yash
Yash - avatar
0
Why did you remove them? I said to put double underscores.
12th May 2020, 6:30 PM
Russ
Russ - avatar
0
i did put double underscores, but i was getting the same error and the output was not asking or an input. but when i removes them and ran it again, it asked for the output and then showed the error
12th May 2020, 6:32 PM
Yash
Yash - avatar
0
OOHHH nvm i got it
12th May 2020, 6:33 PM
Yash
Yash - avatar
0
Thanks for your help Russ
12th May 2020, 6:33 PM
Yash
Yash - avatar
0
Why did we add the double underscores??
12th May 2020, 6:34 PM
Yash
Yash - avatar
0
__add__ is the Magic Method for the "+" sign. When you create classes, you might need to define behaviour between two objects. When a + b is executed, it looks to the class magic method __add__ to find out what to do.
12th May 2020, 6:37 PM
Russ
Russ - avatar
0
Oh Okay, thanks!!
12th May 2020, 6:39 PM
Yash
Yash - avatar