索引100超出尺寸100的轴0的范围 [英] index 100 is out of bounds for axis 0 with size 100
问题描述
我正尝试使用python编写一个程序来计算
I was trying to write a programme using python to compute the infinite series of
1/1 ^ 2 + 1/2 ^ 2 + 1/3 ^ 2 + 1/4 ^ 2 + .....
1/1^2 + 1/2^2 + 1/3^2 +1/4^2 +.....
我的代码如下:
n = 100
x = np.zeros([n])
x[0] = 0
for i in range(n):
x[i+1] = x[i] + 1/float((i+1)**2)
print x[99]
当我尝试执行代码时,它返回以下内容:
when I tried to execute the code, it returned something as:
IndexError:索引100超出了轴100的大小100的范围
IndexError: index 100 is out of bounds for axis 0 with size 100
知道代码有什么问题。谢谢!:)
I would like to know what is wrong with the code. Thanks!:)
推荐答案
索引 i
升至 99
,因此您尝试在最后一次迭代中获得 x [i + 1] == x [100]
不起作用,因为 x
升至 x [99]
Index i
goes up to 99
so you are trying to get x[i + 1] == x[100]
at the last iteration, so it can not work because x
goes up to x[99]
在您的for循环中只需执行 range(n-1)
In your for loop just do range(n - 1)
这篇关于索引100超出尺寸100的轴0的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!