This program is supposed to add, subtract, multiply, and divide complex numbers and doubles as well as complex numbers against complex numbers. When i try to run it, I get a syntax error at line 59 and cannot figure out why. Any help/advice/pointers would be greatly appreciated.

class complex:
    def __init__(self, realPart=1, imaginaryPart=2):
        self.realPart = realPart
        self.imaginaryPart = imaginaryPart

    def __add__(self, other):
        if isinstance(other, double):
            return complex(self.realPart+other, self.imaginaryPart)
        elif isinstance(other, complex):
            return complex(self.realPart + other.realPart, self.imaginaryPart + other.imaginaryPart)
        else:
            raise TypeError

    def __truediv__(self, other):
        if isinstance(other, double):
            return complex((self.realPart*other)/(other**2), (self.imaginaryPart*other)/(other**2))
        elif isinstance(other, complex):
            return complex(((self.realPart * other.realPart) + (self.imaginaryPart * other.imaginaryPart))/(other.realPart**2 + other.imaginaryPart**2), (self.imaginaryPart*other.realPart - self.realPart*other.imaginaryPart)/(other.realPart**2 + other.imaginaryPart**2))
        else:
            raise TypeError

    '''def __float__(self):
        return float(self.num) / self.den

    def __int__(self):
        return self.num / self.den'''

    def __mul__(self, other):
        if isinstance(other, double):
            return complex(self.realPart*other, self.imaginaryPart*other)
        elif isinstance(other, complex):
            return complex(self.realPart*other.realPart - self.imaginaryPart*other.imaginaryPart, self.realPart*other.imaginaryPart + self.imaginaryPart*other.realPart)
        else:
            raise TypeError

    def __radd__(self, other):
        return self + other

    def __rtruediv__(self, other):
        return complex(other) / self

    def __rmul__(self, other):
        return self * other

    def __rsub__(self, other):
        return complex(other) - self

    def __str__(self):
        return '(' + str(self.realPart) + ' + ' + str(self.imaginaryPart) + 'i' + ')'

    def __sub__(self, other):
        if isinstance(other, double):
            return complex(self.realPart-other, self.imaginaryPart)
        elif isinstance(other, complex):
            return complex(self.realPart - other.realPart, self.imaginaryPart - other.imaginaryPart)
        else:
            raise TypeError

if __name__ == '__main__':

    a = complex(2, 5)
    b = complex(1, 3)

    i = 4

    print('%s + %s = %s' % (a, b, a + b))
    print('%s - %s = %s' % (a, b, a - b))
    print('%s * %s = %s' % (a, b, a * b))
    print('%s / %s = %s' % (a, b, a / b))

    print('%s + %i = %s' % (a, i, a + i))
    print('%s - %i = %s' % (a, i, a - i))
    print('%s * %i = %s' % (a, i, a * i))
    print('%s / %i = %s' % (a, i, a / i))

    print('%i + %s = %s' % (i, a, i + a))
    print('%i - %s = %s' % (i, a, i - a))
    print('%i * %s = %s' % (i, a, i * a))
    print('%i / %s = %s' % (i, a, i / a))

There are many possible problems with this code, one of which is the following. Add some print statements to see what the program is doing.

def __radd__(self, other):
        print "__radd__ return =", self, other, type(self), type(other)
        return self + other
 
    def __rtruediv__(self, other):
        ## add a similar print  statement here
        return complex(other) / self
 
    def __rmul__(self, other):
        ## add a similar print  statement here
        return self * other
 
    def __rsub__(self, other):
        ## add a similar print  statement here
        return complex(other) - self

Also here

a = complex(2, 5)
    b = complex(1, 3)
 
    i = 4
 
    print type(a), type(b)
    print('%s + %s = %s' % (a, b, a + b))

I would suggest that you start with one function, let us say adding, get it to work and then go on from there as none of the code you posted will work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.