0

the error returned is "IndexError: index 1 is out of bounds for axis 0 with size 1" I am not sure how to fix this, please help I am very new to python

nsteps7 = 1

def randomwalk7(nsteps7):

    position = 0
    walk7 = [position]

    x = np.transpose(walk7)[0]
    y = np.transpose(walk7)[1]

    x, y = 0,0
    walk7x, walk7y = [x], [y]
    for i in range(nsteps7):

        val = random.randint(1,5)
        if val == 1:
            x += 1
        elif val == 2:
            y += 1
        elif val == 3:
            x += -1
        else:
            y += -1
        walk7x.append(x)
        walk7y.append(y)
    return[walk7x, walk7y]

numwalks7 = 5
walks7 = [randomwalk7(nsteps7) for i in range(numwalks7)]

plt.hist(walks7)
Clément
  • 1,128
  • 7
  • 21
Tefers
  • 1
  • 1
  • You might want to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) for that or look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Nov 04 '19 at 06:05

1 Answers1

0

What do you want to do with your tranposition? Here, you are trying to get the second element of the list y which is walk7 transposed but there is only one element in walk7. So for this specific case you have walk7 = y = [position] = 0

Since you want to get the second element of a 1-element size list : you are out-of-bound

Clément
  • 1,128
  • 7
  • 21