In the below code I am not getting the desired result:
#!/bin/python3
import math
import os
import random
import re
import sys
class comp:
def __init__(self,real,img):
self.real=real
self.img=img
def add(self,p2):
r= p1.real+p2.real
i= p1.img+p2.img
print("Sum of the two Complex numbers :"+str(r)+'+'+str(i)+'i')
def sub(self,p2):
r= p1.real-p2.real
i= p1.img-p2.img
print("Subtraction of the two Complex numbers :"+str(r)+'+'+str(i)+'i')
if __name__ == '__main__':
real1 = int(input().strip())
img1 = int(input().strip())
real2 = int(input().strip())
img2 = int(input().strip())
p1 = comp(real1,img1)
p2 = comp(real2,img2)
p1.add(p2)
p1.sub(p2)
The code works but when the imaginary field takes a result in -2, it is printing the result as +-2i
Result eg: 1+2i - 3+4i = -2-2i (but as it is hard coded as "+" in the comment it is resulting in "-2+-2i"
How can I get rid of it?