When you run your code, you get the same exception:
Traceback (most recent call last): File ".../stack.py", line 28, in <module> addVectors(v1,v2) File ".../stack.py", line 15, in addVectors sum_vec.extend( vec1[i] + vec2[i] ) TypeError: 'int' object is not iterable
In other words, the extend method expects an iterative argument. However, the append method receives the element as an argument.
Here is a small example of the difference between extension and addition:
l = [1, 2, 3, 4] m = [10, 11] r = list(m) m.append(l) r.extend(l) print(m) print(r)
Output:
[10, 11, [1, 2, 3, 4]] [10, 11, 1, 2, 3, 4]
Sam bruns
source share