4 Replies - 306 Views - Last Post: 25 October 2017 - 08:33 AM Rate Topic: -----

#1 blablabla333  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-July 17

List Index out of Range

Posted 24 October 2017 - 05:47 AM

Hi guys,
N=3 (will be 2 at most in the loop I want to create)
T=5 (will start from 2 in the loop I want to create)
I want to print:
x(1)(2),I(1)(1)
x(1)(3),I(1)(2)
x(1)(4),I(1)(3)
x(1)(5),I(1)(4)
x(2)(2),I(2)(1)
x(2)(3),I(2)(2)
x(2)(4),I(2)(3)
x(2)(5),I(2)(4)

I am getting list index out of range error , can you help me or fix it ?
for ma in range(1,N):
 for a in range(2,T+1):
thevars=[x[ma][a],I[ma][a-1]]
print(thevars)


Is This A Good Question/Topic? 0
  • +

Replies To: List Index out of Range

#2 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: List Index out of Range

Posted 24 October 2017 - 05:56 AM

The indenting is wrong. Line 3 should be indented within the a-loop, as should the print statement presumably. If that is your actual code, though, then I'm surprised that it runs because the a-loop lacks any statement.

If it is not the actual code then just copy and paste it, don't attempt to retype it.

Note also that list indexes start from one. (Or maybe you are ignoring the zeroth elements, but we don't know this from what you've provided.)

For further assistance you'll need to provide more than those 4 lines of code.
Was This Post Helpful? 0
  • +
  • -

#3 blablabla333  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-July 17

Re: List Index out of Range

Posted 24 October 2017 - 06:02 AM

for ma in range(1,N):
        for a in range(2,T+1):
            thevars=[x[ma][a],I[ma][a-1]]
            print(thevars)

In the original code, I do not have any intending error. Why shoul I something for a_loop ? I just wanted to print the variables and I know there is a problem indexing ; I was asking how can I correct it ?

This post has been edited by andrewsw: 24 October 2017 - 06:06 AM
Reason for edit:: fixed botched code tags and removed previous quote (use the Reply button)

Was This Post Helpful? 0
  • +
  • -

#4 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 590
  • View blog
  • Posts: 2,487
  • Joined: 13-June 14

Re: List Index out of Range

Posted 24 October 2017 - 10:41 PM

Since you haven't shown where x or I come from, how are we supposed to know what are valid indices for their dimensions? As pointed out before, you're going to need to show more code.
Was This Post Helpful? 0
  • +
  • -

#5 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 321
  • View blog
  • Posts: 1,056
  • Joined: 27-December 13

Re: List Index out of Range

Posted 25 October 2017 - 08:33 AM

This provides the exact output in your request, but I have no idea as to weather this is what you are looking for...
N = 3
T = 5
for ma in range(1,N):
     for a in range(2,T+1):
         print('x(', ma,')(',a,'),I(',ma,')(',a-1,')')


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1