代码与前面的代码相同,但是会出错?

时间:2018-05-01 19:07:28

标签: python function random-walk

我一直在

  

TypeError:' int'对象不可订阅

在我的uniform()函数中。如果我注释掉均匀函数,equall()和gauss()工作正常,我所做的只是从它们复制代码并使用我的stepe()函数而不是step()或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 个答案:

答案 0 :(得分:1)

def evenly(walk,steps):

此处参数walk(而不是walks)使用相同的名称屏蔽外部变量,并为后续语句walk[s+1] = walk[s] + stepn()定义。我想它应该是walks