Page 1 of 1

## 2 Replies - 1583 Views - Last Post: 03 November 2012 - 04:08 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=298438&amp;s=edcdd8808de4871a519bf2322b764e0d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 fledgeling

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

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

### #2 fledgeling

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

Posted 03 November 2012 - 02:54 AM

fledgeling, 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

### #3 fledgeling

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

Posted 03 November 2012 - 04:08 AM

fledgeling, on 03 November 2012 - 02:54 AM, said:

fledgeling, 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

```