Reputation: 37
I am a beginner in Python . While running the following code.
from array import *
x=[]
x[0]=.232
for i in range(25):
x[i+1]=1/[i+1]-5*x[i]
end
I get an error:
x[0]=.232 IndexError: list assignment index out of range
Can someone help me sort this out
Upvotes: 0
Views: 76
Reputation: 1807
x=[] makes an empty list so you can't call x[0], so make a list of 26 elements(you need all total 26 elements) all equal to zero for convenience,
x=[0.0]*26
or
x=[0.0 for i in range(26)]
again [i+1]
denotes a list and you cant do calculation with that make (i+1). also 1/(i+1)
gives integer division make 1.0/(i+1)
end
is not a valid python function here, dont use it indente the next line under the for loop. final programme,
x=[0.0]*26
x[0]=.232
for i in range(25):
x[i+1]=1.0/(i+1)-5*x[i]
Upvotes: 0
Reputation: 1930
Your code has more errors, but in this particular case you are trying to access the first position (x[0]
) of an empty array (declared as x=[]
)
The same error appears in the loop (x[i+1]
is index out of range since the array is empty) and you have a syntax error, end
is not a python keyword. Finally, the body of the loop should be indented.
Upvotes: 1