【发布时间】:2020-08-27 22:49:45
【问题描述】:
我想创建一个函数来生成具有相应系数的多项式。
class Polynomial:
def __init__(self, *coefficients):
self.coefficients = coefficients
def __len__(self):
return len(self.coefficients)
def __repr__(self):
equation = ""
for i in range(len(self)):
if i == 0:
equation += "(" + str(self.coefficients[i]) + "x^" + str(i)
else:
equation += "+" + str(self.coefficients[i]) + "x^" + str(i)
return equation + ")"
def __lt__(self, other):
return self.coefficients < other.coefficients
def __ge__(self, other):
return self.coefficients >= other.coefficients
a = Polynomial(1,2,3)
print(a)
我希望打印 '(1x^0+2x^1+3x^2)',但它只是 (1x^0)。 我的代码会出现什么问题?提前谢谢你。
【问题讨论】: