Index out of range Python NUmerical Iteration - python

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

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.

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]

Related

How do I fix the error Native JS does not support indexing when trying to append an element into a list python

Given a sequence of non-negative integers, where each number is written in a separate line. The sequence ends with 0. Print the index of the first maximum of the sequence.
so I am trying to append those element into a list, and encouters the error Native JS does not support indexing, how do I fix it?
n = -1
lista = []
while n!= 0:
n = int(input())
lista.append[n]
print(max(lista), lista.index(max(lista)))
this is my idea, but lista.append[n] is untrue
From a quick scan of your code I see you're not calling the append() method correctly. lista.append[n] should be lista.append(n)

Populating a 2D list

I'm trying to enter a 100 number square into a 2D list https://i.stack.imgur.com/eSFiO.png with each list containing 10 numbers 1-10,11-20,21-30 and so on. This is my code so far but when I run it in my editor it just keeps running and eventually crashes without printing anything. Please explain to me what I am doing wrong. Thanks
number_square=[[],[],[],[],[],[],[],[],[],[]]
number_list=[1,2,3,4,5,6,7,8,9,10]
for row in number_square:
for number in number_list:
number_square.append(number)
number_list.remove(number)
number_list.append(number+10)
print(number_square)
That's because you aren't accessing the content of neither row nor number in the for loops. Here is a suggestion:
number_square=[[],[],[],[],[],[],[],[],[],[]]
number_list=[1,2,3,4,5,6,7,8,9,10]
i = 0
for row in number_square:
for i in range(len(number_list)):
row.append(number_list[i])
number_list[i] += 10
print(number_square)
Note that the first loop is equivalent to for each. In this syntax, you can't alter the value of the item in a list. In the second loop, I put a "traditional" for loop with range to alter the values in number_list.
There are many changes required in your code. Good effort from your side on trying the code. I have modified the code and probably you can get the logic from it.
number_square=[[],[],[],[],[],[],[],[],[],[]]
number_list=[10,20,30,40,50,60,70,80,90,100]
for i in range(0,len(number_square)):
for j in range(number_list[i]-9,number_list[i]+1):
number_square[i].append(j)
Problems:
Removing from number_list while iterating over it
Appending to number_square instead of row
If I were you, I'd use a list comprehension with ranges instead:
number_square = [list(range(i, i+10)) for i in range(1, 101, 10)]
Due credit to Onyambu for suggesting something similar in a comment

IndexError: string index out of range - How do I get this error when the index does not go outside the range of called list?

Inside a method, I am calling all of the elements inside a nested list. Here is the code where the error message leads me:
def code_from_conditions(self):
for i in range(0, len(self.list_of_lists[2])):
if self.list_of_lists[2][i] == "Yes":
self.list_of_lists[2][i] = "tfa_3895"
else:
self.list_of_lists = str(0)
Specifically, it is this code right here:
if self.list_of_lists[2][i] == "Yes":
Now, my first instinct was to change this line:
for i in range(0, len(self.list_of_lists[2])):
to this:
for i in range(0, len(self.list_of_lists[2]) - 1):
But after getting the same error message, I've decided to test whether or not I can print all of the elements inside the list I am calling:
def code_from_conditions(self):
for i in range(0, len(self.list_of_lists[2])):
print(self.list_of_lists[2][i])
and it works! So if I am doing basically the same thing which is calling all elements inside a list but it is inside an if/else statement, then why do I get the error?
In the else branch you assign self.list_of_lists = str(0). This means that in the be self.list_of_lists is no longer list of lists but "0" and therefore this will yield IndexOutOfRange error since len(self.list_of_lists is 1.
The string index out of range means that the index you are trying to access does not exist. In a string, that means you're trying to get a character from the string at a given point. If that given point does not exist , then you will be trying to get a character that is not inside of the string.
How do I get this error when the index does not go outside the range of called list?
makes no sense.

My code points that my list is out of range on line 9. Could someone tell me how to fix it?

v_leader=[]
v_follower=[]
distance=[]
t_s=float(input("Enter the value of the time stamp"))
r_t=float(input("Enter the vlaue of the reaction time"))
i=t_s
n=1
while i<12.5:
v_leader[n]=v_leader[n-1]+8*t_s
i+=i
n+=1
m=n+1
while m<n+11:
v_leader[m]=v_leader[m-1]
t=m+1
while t<m+11:
v_leader[t]=v_leader[t-1]-10*t_s
print(v_leader)
I am trying to fix the IndexError raised by this line of code:
v_leader[n]=v_leader[n-1]+8*t_s
All your lists are initiated as empty lists so in the looping process, your list will be out of range. You can use try...except to avoid this.
You cannot access indexes greater than the length of the list. As all of your lists start out with 0 elements, calling v_leader[n] will always throws an error because there are no elements to access.
This can be fixed if, in the first loop, you ditch the counter n, and simply append new values.
while i<12.5:
v_leader[n] += [v_leader[n-1]+8*t_s]
i+=i
In your next loop, you can simply call something like while m < len(v_leader) + 11:.

Having adding to dictionary problems python

This is part of my code:
add_values=[2,5,10,20,50,100,200,500,1000,2000,5000,10000,20000,50000]
for each in add_values:
print(each)
s=add_values[each]
s=int(s)
h=s*100
mydict[add_values[each]]=s
And it is bringing up this error:
IndexError: list index out of range
(For the s=add_values[each] line)
Please can you tell what is wrong here and what needs changing,
Thanks.
Think about reaching the fifth item in add_values:
for each in add_values:
print(each) # each == 50
s=add_values[each] # what's the fiftieth item in 'add_values'?!
You don't need to index into add_values, you are already accessing the value - replace add_values[each] with, simply, each.
each is the value, you can't use it as an index (mainly because the size of add_values is 14 and you have values that are greater than this inside add_values):
add_values=[2,5,10,20,50,100,200,500,1000,2000,5000,10000,20000,50000]
for each in add_values:
print(each)
s=each
s=int(s)
h=s*100
mydict[each]=s
Another solution would be to use an index:
add_values=[2,5,10,20,50,100,200,500,1000,2000,5000,10000,20000,50000]
for i in range(len(add_values)):
print(add_values[i])
s=add_values[i]
s=int(s)
h=s*100
mydict[add_values[i]]=s
You are using an array element as an array index, which is why you are getting an out-of-bounds error.
With Python's for loop notation, you don't need to access an index explicitly; just access the element, which in your case is each:
for each in add_values:
print(each)
s=each # kind of redundant, but this should work
for each in add_value sets each to 2, 5, 10, 20, 50, etc. On the 4th iteration of the loop, each is 20. When you say add_values[each], you get an error because add_values has only 14 elements and you're trying to access element number 20. You'd get in even more trouble if it tried to access element number 50000.

Categories

Resources