i am a beginner to Python language and i am facing a problem in passing arguments(if the input is taken by user) to functions in python. The error which i am getting is AttributeError: 'Complexno' object has no attribute 'a'
here is my code: P.S. Correct me if i am wrong somewhere (please help i m stuck )
class Complexno:
def add(self,a,c ,b,d):
sum1=self.a+self.c
sum2=self.b+self.d
print(sum1+" + "+sum2)
def sub(self,a,c,b,d):
sub1=self.a-self.c
sub2=self.b-self.d
print(sub1+" - "+sub2)
def mul(self,a,b,c,d):
mul1=self.a*self.c
mul2=self.b*self.c
print(mul1+" * "+mul2)
def div(self,a,b,c,d):
div1=self.a/self.c
div2=self.b/self.d
print(div1+" / "+div2)
a=float(input("Enter the real part of first no"))
b=float(input("Enter the imaginary part of first no"))
c=float(input("Enter the real part of second no"))
d=float(input("Enter the imaginary part of second no"))
ob1=Complexno()
ob1.add(a,b,c,d)
ob1.sub(a,b,c,d)
ob1.div(a,b,c,d)
ob1.mul(a,b,c,d)
You are referencing a class variable that you have not set.
You will not have this problem anymore if you remove the self
references within the methods.
class Complexno:
def add(self,a,c ,b,d):
sum1= a + c
sum2= b + d
print(sum1+" + "+sum2)
def sub(self,a,c,b,d):
sub1= a - c
sub2= b - d
print(sub1+" - "+sub2)
def mul(self,a,b,c,d):
mul1= a * c
mul2= b * c
print(mul1+" * "+mul2)
def div(self,a,b,c,d):
div1= a / c
div2= b / d
print(div1+" / "+div2)
A better solution might be to create a complex number class, initializing it with the real and imaginary parts and then overload the operations you care about to return another complex number.
class Complexno:
def __inti__(self, real, imaginary):
self.a = real
self.b = imaginary
def __str__(self):
return "{}+{}i".format(self.a, self.b)
def __add__(self, other):
real = self.a + other.a
imaginary = self.b + other.b
return Complexno(real, imaginary)
def __sub__(self, other):
real = self.a - other.a
imaginary = self.b - other.b
return Complexno(real, imaginary)
def __mul__(self, other):
real = self.a * other.a
imaginary = self.b * other.b
return Complexno(real, imaginary)
def __floordiv__(self, other):
real = self.a // other.a
imaginary = self.b // other.b
return Complexno(real, imaginary)
def __truediv__(self, other):
real = self.a / other.a
imaginary = self.b / other.b
return Complexno(real, imaginary)
But if you are creating your own class, you will probably want to implement even more methods than this.
See this page for an example of methods to add, however, it looks like that site is written for 2.7 because they use __div__
instead of the two division methods needed for 3.x.
Using this class, you can then perform the operations like this:
a = float(input("Enter the real part of first no"))
b = float(input("Enter the imaginary part of first no"))
c = float(input("Enter the real part of second no"))
d = float(input("Enter the imaginary part of second no"))
num1 = Complexno(a, b)
num2 = Complexno(c, d)
print(num1 + num2)
print(num1 - num2)
print(num1 * num2)
print(num1 / num2)