2 Replies - 870 Views - Last Post: 03 November 2012 - 04:08 AM Rate Topic: -----

#1 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

List index out of range error, please help

Posted 03 November 2012 - 02:14 AM

So, this is my code, can ayone please explain me why i get this error ?
" if L[c]==0:
IndexError: list index out of range"

while True:
    n = int(raw_input("matrix dimension "))
    if n > 2 and n % 2!=0:
        break

L = [0] * (n*n)
c=n/2
L[c]=1
print L
for i in range(2,n**n-2):
    if c==n-1:
        esc=c+n
        c= n*(n-1)
        if L[c]==0:
            L[c]=i
        else:
            c=esc
            L[c]=i
    else :
        for x in range (n):
            if c < n-1 + x*n:
                Next=True
                if c-n-1>0:
                    c=c-n+1
                    if L[c]==0:
                    
                        L[c]=i
                    else:
                        c=c+n-1
                        L[c]=i
                        break
                else:
                    esc=c+n
                    z=abs(c-n-1)%n
                    c=n*n-z
                    if L[c]==0:
                        L[c]=i
                    else:
                        c=esc
                        L[c]=i
                    break
        if Next==True:
            for x in range (n):
                if c==n-1+x*n:
                    esc=c+n
                    c=c-(n+(n-1))
                    if L[c]==[0]:
                        L[c]=i
                    else:
                        c=esc
                        L[c]=i
                break
print L                    



Is This A Good Question/Topic? 0
  • +

Replies To: List index out of range error, please help

#2 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

Re: List index out of range error, please help

Posted 03 November 2012 - 02:54 AM

View Postfledgeling, on 03 November 2012 - 02:14 AM, said:

So, this is my code, can ayone please explain me why i get this error ?
" if L[c]==0:
IndexError: list index out of range"

while True:
    n = int(raw_input("matrix dimension "))
    if n > 2 and n % 2!=0:
        break

L = [0] * (n*n)
c=n/2
L[c]=1
print L
for i in range(2,n**n-2):
    if c==n-1:
        esc=c+n
        c= n*(n-1)
        if L[c]==0:
            L[c]=i
        else:
            c=esc
            L[c]=i
    else :
        for x in range (n):
            if c < n-1 + x*n:
                Next=True
                if c-n-1>0:
                    c=c-n+1
                    if L[c]==0:
                    
                        L[c]=i
                    else:
                        c=c+n-1
                        L[c]=i
                        break
                else:
                    esc=c+n
                    z=abs(c-n-1)%n
                    c=n*n-z
                    if L[c]==0:
                        L[c]=i
                    else:
                        c=esc
                        L[c]=i
                    break
        if Next==True:
            for x in range (n):
                if c==n-1+x*n:
                    esc=c+n
                    c=c-(n+(n-1))
                    if L[c]==[0]:
                        L[c]=i
                    else:
                        c=esc
                        L[c]=i
                break
print L                    



I found kind of many errors on my code so i take back what i asked please delete this post, i am sorry for the truble
Was This Post Helpful? 0
  • +
  • -

#3 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

Re: List index out of range error, please help

Posted 03 November 2012 - 04:08 AM

View Postfledgeling, on 03 November 2012 - 02:54 AM, said:

View Postfledgeling, on 03 November 2012 - 02:14 AM, said:

So, this is my code, can ayone please explain me why i get this error ?
" if L[c]==0:
IndexError: list index out of range"

while True:
    n = int(raw_input("matrix dimension "))
    if n > 2 and n % 2!=0:
        break

L = [0] * (n*n)
c=n/2
L[c]=1
print L
for i in range(2,n**n-2):
    if c==n-1:
        esc=c+n
        c= n*(n-1)
        if L[c]==0:
            L[c]=i
        else:
            c=esc
            L[c]=i
    else :
        for x in range (n):
            if c < n-1 + x*n:
                Next=True
                if c-n-1>0:
                    c=c-n+1
                    if L[c]==0:
                    
                        L[c]=i
                    else:
                        c=c+n-1
                        L[c]=i
                        break
                else:
                    esc=c+n
                    z=abs(c-n-1)%n
                    c=n*n-z
                    if L[c]==0:
                        L[c]=i
                    else:
                        c=esc
                        L[c]=i
                    break
        if Next==True:
            for x in range (n):
                if c==n-1+x*n:
                    esc=c+n
                    c=c-(n+(n-1))
                    if L[c]==[0]:
                        L[c]=i
                    else:
                        c=esc
                        L[c]=i
                break
print L                    



I found kind of many errors on my code so i take back what i asked please delete this post, i am sorry for the truble







After many corrections i came up with this code but i still get an error like that in line 64
if i just erase the line with error the program simply shows me no result, can you please help me figure out what is going wrong ?

while True:
    n = int(raw_input("matrix dimension "))
    if n > 2 and n % 2!=0:
        break

L = [0] * (n*n)
c=n/2
L[c]=1
num=2
while num<n*n:

    Next=True
    if c==n-1:
        esc=c+n
        c= n*(n-1)
        if L[c]==0:
            L[c]=num
            num=+1
        else:
            c=esc
            L[c]=num
            num+=1
    else :
        for x in range (n):
            if c < n-1 + x*n:
                Next=False
                if c-n+1>0:
                    esc=c+n
                    c=c-n+1
                    if L[c]==0:
                    
                        L[c]=num
                        num+=1
                        break
                    else:
                        c=esc
                        L[c]=num
                        num+=1
                        break
                else:
                    esc=c+n
                    z=abs(c-(n-1))%n
                    c=n*n-z
                    if L[c]==0:
                        L[c]=num
                        num+=1
                        break
                    else:
                        c=esc
                        L[c]=num
                        num+=1
                        break
        if Next==True:
            for x in range (n):
                if c==n-1+x*n:
                    esc=c+n
                    c=c-(n+(n-1))
                    if L[c]==[0]:
                        L[c]=num
                        num+=1
                        break
                    else:
                        c=esc
                        L[c]=num
                        num+=1
                        break
                 
print L                    


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1