-3

I'm trying to have a function take an array and int as inputs and return an array of the same length after doing some calculations.

I'm completely new to Python and may just be making a very obvious mistake but can't resolve it on my own.

The error message says im out of range but ive tried shortening and expanding the range of the for loop?

def WMA(element, duration):

    multiplier = 1 / duration

    z = [len(element)]
    z[0] = element[0] * multiplier

    for k in range(1, len(element) + 1):
        z[k] = (element[k] * multiplier) + (z[k - 1] * (1 - multiplier))
    return z

Error message:

IndexError: list assignment index out of range
smci
  • 32,567
  • 20
  • 113
  • 146
cfrost6
  • 13
  • 3
  • z only has one element, so you can’t assign to z[k]. – quamrana Aug 25 '19 at 21:06
  • `... in range(1, len(element) + 1)` and then `element[k]`. You will end up trying to access an out-of-bounds/out-of-range member of `element`. – Spencer D Aug 25 '19 at 21:09
  • Python (and most languages other than MATLAB, R) indexes things from 0..(L-1) not from 1..L. Hence `range(1, len(element) + 1)` is wrong, you meant simply `range(len(element))`. – smci Aug 25 '19 at 21:30
  • The issue has nothing to do with the function, we don't need the line with the function definition to reproduce the issue. (For future please only post the [MCVE (minimal, complete, verifiable example)](http://stackoverflow/help/mcve)) – smci Aug 25 '19 at 21:40
  • Your line `z = [len(element)]` doesn't make much sense, it is surely a typo for `z = [0] * len(element)` which would create a list of specified length, as others previously pointed out. Probably someone misunderstood/mistranscribed that code. – smci Aug 25 '19 at 22:02

3 Answers3

2

This line should fix it:

z = [0] * len(element)

and later:

for k in range(1, len(element)):

as Ronald points out.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • Aha! This worked. Just changing it to len(element) instead of len(element) + 1 didn't quite solve my issue but changing z = [len(element)] to [0] * len(element) as well did! Thank you for your help (I have experience in MATLAB and was able to accomplish this no problem using that platform but am trying to branch out and learn python now) – cfrost6 Aug 25 '19 at 21:30
1

You have for k in range(1, len(element) + 1):

Let's say element has N items in its list so that len(element) == N. In Python, the first element of the list is element[0] and the last would be element[N - 1]. But you are letting k take on values of 1, 2, ... N. When k is N, you will have in IndexError exception.

You should change the statement to be:

for k in range(1, len(element)):
Booboo
  • 38,656
  • 3
  • 37
  • 60
0

Try this:

You shouldn't need to add one to len(element) in the range function of the for loop since the range will start at zero and go up to len(element)-1, adding one will make it IndexOutOfRange:

For example

    l = [1,2,3]

    print(len(l))

3


    for i in range(len(l)):
        print(i)


0
1
2

    for i in range(len(l)+1):
        print(i)


0
1
2
3

    for i in range(len(l)):
        print(l[i])


1
2
3

    for i in range(len(l)+1):
        print(l[i])


1
2
3

    Traceback (most recent call last):
      File "<pyshell#23>", line 2, in <module>
        print(l[i])
    IndexError: list index out of range