1

I keep getting

TypeError: 'int' object is not subscriptable

within my evenly() function. equall() and gauss() work fine if I comment out the evenly function, and all I did was copy the code from them and used my stepe() function instead of step() or stepn()

import numpy as np
import matplotlib.pyplot as plt
from numpy.random import rand,seed,randn

steps = 1
walks = 10000

walk = np.zeros(steps + 1)

def step():
    if rand() < 0.5:
        return 1.0
    return -1.0

def stepn():
    return randn() + 0.5

def stepe():
    return (6*(rand()-0.5))

def equall(walks,steps):
    l = []
    for w in range(walks):
        for s in range(steps):
            walk[s+1] = walk[s] + step()
        l.append(walk[steps])
    return l

def gauss(walks,steps):
    l = []
    for w in range(walks):
        for s in range(steps):
            walk[s+1] = walk[s] + stepn()
        l.append(walk[steps])
    return l

def evenly(walk,steps):
    l = []
    for w in range(walks):
        for s in range(steps):
            walk[s+1] = walk[s] + stepn()
        l.append(walk[steps])
    return l

plt.figure(figsize=(10,7))
plt.hist(equall(walks,steps),bins=15)
plt.show()
plt.hist(gauss(walks,steps),bins=15)
plt.show()
plt.hist(evenly(walks,steps),bins=15)
plt.show()

1 Answers1

1
def evenly(walk,steps):

Here parameter walk (instead of walks) masks outer variable with the same name, and is defined for subsequent statement walk[s+1] = walk[s] + stepn(). I guess it should be walks

Blownhither Ma
  • 1,461
  • 8
  • 18