0
我正在写一个多项式类,并有我的运营商的GetItem麻烦 这是我为我的GetItem代码:为什么我的方法操作返回没有
def __getitem__(self, idx):
for i in range(len(self.coeffs)):
if self.coeffs[i][1] == idx:
return (self.coeffs[i][0])
break
这里是我的测试代码:
for i in range(P1.degree() + 1):
if P1[i] != 0:
print(P1[i], end = " ")
多项式为6×14^+ 9X^11-12x^3 + 42并将其返回所有的非零系数但是我得到这样
42 None None -12 None None None None None None None 9 None None 6
输出
所以我的问题是我怎么才能只显示coeffients,也为什么没有出现反正呢?
如果你的'if'语句永远不匹配,当函数结束时'None'将被返回。 – BrenBarn
1)当函数结束时,所有的python函数隐式返回'None'。 2)您可以无条件地打印**所有**值。为什么不应该打印'无',如果那是'P1 [i]'的值? –
['return'](https://docs.python.org/3.6/reference/simple_stmts.html#return)后面的'break'是不必要的,因为'return'已经离开了当前函数。 –