Skip to content

Commit 45757da

Browse files
committed
improved vector add solution
1 parent 6d366bd commit 45757da

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

exercises/vector/solution/vector.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,12 @@ def __neg__(self):
6161

6262
def __add__(self, other):
6363
try:
64-
if len(self) != len(other):
65-
raise ValueError('Only vectors of same length may be added.')
66-
return Vector(a + b for a, b in zip(self, other))
64+
other = Vector(other)
6765
except TypeError:
6866
return NotImplemented
67+
if len(self) != len(other):
68+
raise ValueError('vectors must have same length for addition')
69+
return Vector(a + b for a, b in zip(self, other))
6970

7071
def __radd__(self, other):
7172
return self + other
72-
73-
74-
75-

0 commit comments

Comments
 (0)