gpt4 book ai didi

python - 代码与前面的行相同,但给出了错误?

转载 作者:行者123 更新时间:2023-12-01 09:29:01 25 4
gpt4 key购买 nike

我不断得到

TypeError: 'int' object is not subscriptable

在我的evenly()函数中。如果我注释掉均匀函数, 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()

最佳答案

def evenly(walk,steps):

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

关于python - 代码与前面的行相同,但给出了错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50122358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com