Python Forum
IndexError: list index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: list index out of range
#1


I'm trying to make a program that can work out a mathematical ratio problem and then take the square root of the answer. To achieve the multiplication part i'm trying to make empty lists to which i add the numbers the user enters to define the ratio, then multiplying the list elements with each other. However, when i tried to run the program, this is what i got:

Error:
first set: 8 Traceback (most recent call last): File "C:/root to file...", line 13, in <module> data_procesing() File "C:/root to file...", line 9, in data_procesing() a = parameters[0] * parameters[1] IndexError: list index out of range Process finished with exit code 1
this is the program:

def data_procesing():

    while True:
        first_set = raw_input('first_set: ')
        for i in xrange(1, 2):
            parameters = []
            parameters.append(first_set)

            a = parameters[0] * parameters[1]
            print a


data_procesing()
First i had the parameters = [] loop outside the function so i thought this might be a scope problem, so i put it inside the for loop, however, that didn't work either, Can anybody please tell me what i'm doing wrong? is it perhaps impossible to multiply list elements like i did?
Reply
#2
parameters = []
parameters.append(first_set)
At this point, the list only has one element (first_set).
Trying to access its second element (parameters[1]) results in IndexError.
Reply
#3
Oh yes of course, what was i thinking, thanks a million!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 2,079 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Thumbs Down I hate "List index out of range" Melen 20 3,302 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,302 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,899 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,316 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,434 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,568 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,173 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,832 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,299 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020