如何在return语句中添加间距和逗号

时间:2018-02-21 00:12:46

标签: python complex-numbers

我有一个我正在处理的脚本,并且几个打印命令没有格式化我打算如何。这是其中一条线及其打印出来的示例。

 print(c1, " - ", c2, " = ", c1 - c2)
prints: "5-6.85j"

我试图想出一种在a和b之间添加逗号和空格的方法(在上面的例子中,5和-6.85j)。这是完整的python脚本:

class Complex():
    def __init__(self, a = 0, b = 0):
        self.a = a
        self.b = b
    def __add__(self, secondobject): #addition operator for complex numbers
        return (self.a + secondobject.a) + (self.b + secondobject.b) * 1j 
    def __sub__(self, secondobject): #subtraction operator for complex numbers
        return (self.a - secondobject.a) + (self.b - secondobject.b) * 1j 
    def __mul__(self, secondobject): #multiplucation operator for complex numbers
        return ((self.a * secondobject.a) - (self.b * secondobject.b)) + ((self.b * secondobject.a) + (self.a * secondobject.b)) * 1j
    def __truediv__(self, secondobject): #division operator for complex numbers
        return (((self.a * secondobject.a) + (self.b * secondobject.b))/((secondobject.a ** 2) + secondobject.b ** 2)) + ((((self.b)*(secondobject.a))-((self.a)*(secondobject.b)))*1j)/((secondobject.a ** 2) + secondobject.b ** 2)
    def __abs__(self):
        return abs(self.a + (self.b * 1j))
    def __str__(self):
        return str(self.a) + " " + "+" + " " + str(self.b) + "i"
    def __eq__(self, secondobject):
        if self is secondobject:
            return True
        else:
            return False
    def __ne__(self, secondobject):
        return not self == secondobject
    def __lt__(self, secondobject):
        return abs(self) < abs(secondobject)
    def __le__(self, secondobject):
        return abs(self) <= abs(secondobject)
    def __gt__(self, secondobject):
        return abs(self) > abs(secondobject)
    def __ge__(self, secondobject):
        return abs(self) >= abs(secondobject)

def main():
    input_line = input("Enter the first complex number: ")
    input_line = list(map(float,input_line.split()))
    a, b = input_line[0], input_line[1]
    c1 = Complex(a, b)
    input_line = input("Enter the second complex number: ")
    input_line = list(map(float,input_line.split()))
    a, b = input_line[0], input_line[1]
    c2 = Complex(a, b)

    print()
    print("c1 is", c1)
    print("c2 is", c2)
    print("|" + str(c1) + "| = " + str(abs(c1)))
    print("|" + str(c2) + "| = " + str(abs(c2)))

    print(c1, " + ", c2, " = ", c1 + c2)
    print(c1, " - ", c2, " = ", c1 - c2)
    print(c1, " * ", c2, " = ", c1 * c2)
    print(c1, " / ", c2, " = ", c1 / c2)

    print("Is c1 < c2?", c1 < c2)
    print("Is c1 <= c2?", c1 <= c2)
    print("Is c1 > c2?", c1 > c2)
    print("Is c1 >= c2?", c1 >= c2)
    print("Is c1 == c2?", c1 == c2)
    print("Is c1 != c2?", c1 != c2)
    print("Is c1 == 'Hello There'?", c1 == 'Hello There')
    print("Is c1 != 'Hello There'?", c1 != 'Hello There')

main()

1 个答案:

答案 0 :(得分:0)

修改return语句以返回Complex个对象。例如__add__

def __add__(self, secondobject): #addition operator for complex numbers
    return Complex((self.a + secondobject.a), (self.b + secondobject.b))