Reputation: 3488
I have defined my own linspace command to generate a vector of elements. The iteration over this vector however fails with iteration over non sequence of type int
This is the linspace
def linspace(a, b, n=100):
if n < 2:
return b
diff = (float(b) - a)/(n - 1)
# return [diff * i + a for i in range(n)]
linlist = []
for i in range(n):
linlist.append(diff * i + a)
return linlist
and this my for loop
n = (beginPos - endPos) / diff + 1
positions = linspace(beginPos, endPos, n)
for z in positions:
## Move to new position
MoveTo(z)
I have no idea why the positions variable is not a valid sequence .
EDIT: It was my definition of n the number n was negative, it needs to be calculated as
n = abs((beginPos - endPos) / diff) + 1
Upvotes: 0
Views: 449
Reputation: 369344
If n
is less than 2, linspace
return b
, not a list.
if n < 2:
return b # <----
>>> def linspace(a, b, n=100):
... if n < 2:
... return b
... diff = (float(b) - a)/(n - 1)
... # return [diff * i + a for i in range(n)]
... linlist = []
... for i in range(n):
... linlist.append(diff * i + a)
... return linlist
...
>>> linspace(1, 100, n=1)
100
>>> for z in linspace(1, 100, n=1): pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
Adjust return b
to return a list.
Upvotes: 1