I'm not sure how to do this, but I'm not sure how to do this.
File "/home/leticia/Documentos/python/Poo/polinomio.py", line 30, in p3 = p1 + p2 File "/home/leticia/Documentos/python/Poo/polinomio.py", line 19, in add indexError: list assignment index out of range
In these attempts to solve, I've even added an attribute to pass the vector dimension, but I'm still in it, where am I going wrong?
class Polinomio:
def __init__ (self, termos = None, n = 0):
self.termos = termos or []
self.n = [0] * n
def __len__ (self):
return len(self.termos)
def __setitem__ (self, i, x):
self.termos[i] = x
def __getitem__(self, i):
return self.termos[i]
def __add__ (self, other):
soma = Polinomio(n = self.termos.__len__())
for i in range(self.termos.__len__()):
soma.termos[i] = self.termos[i] + other[i]
return soma
def print (self):
print(self.termos)
p1 = Polinomio([1, 2, 3])
p2 = Polinomio([1, 2, 3])
p2.print()
p3 = Polinomio()
p3 = p1 + p2