Python Forum
I have an index error inline 76 but I write the program in a way that cant reach tha - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: I have an index error inline 76 but I write the program in a way that cant reach tha (/thread-30919.html)



I have an index error inline 76 but I write the program in a way that cant reach tha - abbaszandi - Nov-13-2020

##I have an index error inline 76 but I write the program in a way that cant reach that index
import numpy as np
h=500+29/2
kc=50
ks=5
t=20
h=500
km=(kc+ks)/2
q=10^7
dx=.002
zarayeb=np.zeros([377,377])
savabet=np.zeros([377])
def where(i):
    if i in [53,183,313]:
        return 'paye'
    elif i ==1:
        return 'gushchappaiin'
    elif i in range(2,12):
        return 'marzchap'
    elif i==13:
        return 'gushchapbala'
    elif i in [26,39,52,65,78,91,104,117,286,299,312,325,338,351,364]:
        return 'marzbala'
    elif i==130 or i==260:
        return 'ghushchipbala'
    elif i in[143,156,169,182,195,208,221,234,247]:
        return 'marzchipbala'
    elif i==377:
        return 'gushrastbala'
    elif i in range(366,377):
        return 'marzrast'
    elif i==365:
        return 'gushrastpaiin'
    elif i in [14,27,40,66,79,92,105,118,131,144,157,170,196,209,222,235,248,261,274,287,300,326,339,352]:
        return 'marzpaiin'
    elif i in [129,128,127]:
        return 'marzchipchap'
    elif i in [259,258,257]:
        return 'marzchiprast'
    elif i in [139,152,165,178,191,204,217,230,243]:
        return 'marzchippaiin'
    elif i==126:
        return 'gushchipchappaiin'
    elif i==256:
        return 'gushchiprastpaiin'
    elif i in [142,155,168,181,194,207,220,233,246,141,154,167,180,193,206,219,232,245,140,153,166,179,192,205,218,231,244]:
        return 'chip'
    else:
        return 'bdn'
for i in range(378):
    z=where(i)
    if z=='paye':
        zarayeb[i-1,i-1]=1
        savabet[i-1]=33
    elif z=='gushchappaiin':
        zarayeb[i-1,i-1]=-2
        zarayeb[i-1,i-1+13]=1
        zarayeb[i-1,i-1+1]=1
        savabet[i-1]=0
    elif z=='gushrastpaiin':
        zarayeb[i-1,i-1]=-2
        zarayeb[i-1,i-1-13]=1
        zarayeb[i-1,i-1-1]=1
        savabet[i-1]=0
    elif z=='gushchapbala':
        zarayeb[i-1,i-1]=-(2+h*dx/ks)
        zarayeb[i-1,i-1+13]=1
        zarayeb[i-1,i-1-1]=1
        savabet[i-1]=-h*dx*t/ks
    elif z=='gushrastbala':
        zarayeb[i-1,i-1]=-(2+h*dx/ks)
        zarayeb[i-1,i-1-13]=1
        zarayeb[i-1,i-1-1]=1
        savabet[i-1]=-h*dx*t/ks
    elif 'marzchap':
        zarayeb[i-1,i-1]=-4
        zarayeb[i-1,i-1+13]=2
        zarayeb[i-1,i-1-1]=1
        zarayeb[i-1,i-1+1]=1
        savabet[i-1]=0
    elif 'marzrast':
        zarayeb[i-1,i-1]=-4
        zarayeb[i-1,i-1-13]=2
        zarayeb[i-1,i-1-1]=1
        zarayeb[i-1,i-1+1]=1
        savabet[i-1]=0
    elif 'marzchipbala':
        zarayeb[i-1,i-1]=-(4+2*dx*h/kc)
        zarayeb[i-1,i-1-13]=1
        zarayeb[i-1,i-1+13]=1
        zarayeb[i-1,i-1-1]=2
        savabet[i-1]=-(2*h*dx*t/kc+q*dx**2/kc)
    elif 'marzchipchap':
        zarayeb[i-1,i-1]=-(ks+kc+2*km)
        zarayeb[i-1,i-1-13]=ks
        zarayeb[i-1,i-1+13]=kc
        zarayeb[i-1,i-1-1]=km
        zarayeb[i-1,i-1+1]=km
        savabet[i-1]=-(q*dx**2/2)
    elif'marzchprast':
        zarayeb[i-1,i-1]=-(ks+kc+2*km)
        zarayeb[i-1,i-1-13]=kc
        zarayeb[i-1,i-1+13]=ks
        zarayeb[i-1,i-1-1]=km
        zarayeb[i-1,i-1+1]=km
        savabet[i-1]=-(q*dx**2/2)
##I have an index error inline 76 but I write the program in a way that cant reach that index


RE: I have an index error inline 76 but I write the program in a way that cant reach tha - bowlofred - Nov-13-2020

Line 74 says elif 'marzchap':. Since there is no variable mentioned, this is equivalent to elif True:. There is no case where it will fail.

Earlier your conditionals all compare against z. I suspect you meant to do that here (and the later ones) as well.


RE: I have an index error inline 76 but I write the program in a way that cant reach tha - buran - Nov-13-2020

It hurts just looking at this huge if blocks and I will not refactor it for you.

Didn't see the issues that @bowlofred noticed

as to the error - learn how to debug and how to handle errors. And the Index error message is clear, but anyway i-1+13 will produce invalid index for i >= 366

import numpy as np
h=500+29/2
kc=50
ks=5
t=20
h=500
km=(kc+ks)/2
q=10^7
dx=.002
zarayeb=np.zeros([377,377])
savabet=np.zeros([377])
def where(i):
    if i in [53,183,313]:
        return 'paye'
    elif i ==1:
        return 'gushchappaiin'
    elif i in range(2,12):
        return 'marzchap'
    elif i==13:
        return 'gushchapbala'
    elif i in [26,39,52,65,78,91,104,117,286,299,312,325,338,351,364]:
        return 'marzbala'
    elif i==130 or i==260:
        return 'ghushchipbala'
    elif i in[143,156,169,182,195,208,221,234,247]:
        return 'marzchipbala'
    elif i==377:
        return 'gushrastbala'
    elif i in range(366,377):
        return 'marzrast'
    elif i==365:
        return 'gushrastpaiin'
    elif i in [14,27,40,66,79,92,105,118,131,144,157,170,196,209,222,235,248,261,274,287,300,326,339,352]:
        return 'marzpaiin'
    elif i in [129,128,127]:
        return 'marzchipchap'
    elif i in [259,258,257]:
        return 'marzchiprast'
    elif i in [139,152,165,178,191,204,217,230,243]:
        return 'marzchippaiin'
    elif i==126:
        return 'gushchipchappaiin'
    elif i==256:
        return 'gushchiprastpaiin'
    elif i in [142,155,168,181,194,207,220,233,246,141,154,167,180,193,206,219,232,245,140,153,166,179,192,205,218,231,244]:
        return 'chip'
    else:
        return 'bdn'
for i in range(378):
    try:
        z=where(i)
        if z=='paye':
            zarayeb[i-1,i-1]=1
            savabet[i-1]=33
        elif z=='gushchappaiin':
            zarayeb[i-1,i-1]=-2
            zarayeb[i-1,i-1+13]=1
            zarayeb[i-1,i-1+1]=1
            savabet[i-1]=0
        elif z=='gushrastpaiin':
            zarayeb[i-1,i-1]=-2
            zarayeb[i-1,i-1-13]=1
            zarayeb[i-1,i-1-1]=1
            savabet[i-1]=0
        elif z=='gushchapbala':
            zarayeb[i-1,i-1]=-(2+h*dx/ks)
            zarayeb[i-1,i-1+13]=1
            zarayeb[i-1,i-1-1]=1
            savabet[i-1]=-h*dx*t/ks
        elif z=='gushrastbala':
            zarayeb[i-1,i-1]=-(2+h*dx/ks)
            zarayeb[i-1,i-1-13]=1
            zarayeb[i-1,i-1-1]=1
            savabet[i-1]=-h*dx*t/ks
        elif 'marzchap':
            zarayeb[i-1,i-1]=-4
            zarayeb[i-1,i-1+13]=2
            zarayeb[i-1,i-1-1]=1
            zarayeb[i-1,i-1+1]=1
            savabet[i-1]=0
        elif 'marzrast':
            zarayeb[i-1,i-1]=-4
            zarayeb[i-1,i-1-13]=2
            zarayeb[i-1,i-1-1]=1
            zarayeb[i-1,i-1+1]=1
            savabet[i-1]=0
        elif 'marzchipbala':
            zarayeb[i-1,i-1]=-(4+2*dx*h/kc)
            zarayeb[i-1,i-1-13]=1
            zarayeb[i-1,i-1+13]=1
            zarayeb[i-1,i-1-1]=2
            savabet[i-1]=-(2*h*dx*t/kc+q*dx**2/kc)
        elif 'marzchipchap':
            zarayeb[i-1,i-1]=-(ks+kc+2*km)
            zarayeb[i-1,i-1-13]=ks
            zarayeb[i-1,i-1+13]=kc
            zarayeb[i-1,i-1-1]=km
            zarayeb[i-1,i-1+1]=km
            savabet[i-1]=-(q*dx**2/2)
        elif'marzchprast':
            zarayeb[i-1,i-1]=-(ks+kc+2*km)
            zarayeb[i-1,i-1-13]=kc
            zarayeb[i-1,i-1+13]=ks
            zarayeb[i-1,i-1-1]=km
            zarayeb[i-1,i-1+1]=km
            savabet[i-1]=-(q*dx**2/2)
    except IndexError as er:
        print(f'i = {i}')
        print(er)
Output:
i = 366 index 378 is out of bounds for axis 1 with size 377 i = 367 index 379 is out of bounds for axis 1 with size 377 i = 368 index 380 is out of bounds for axis 1 with size 377 i = 369 index 381 is out of bounds for axis 1 with size 377 i = 370 index 382 is out of bounds for axis 1 with size 377 i = 371 index 383 is out of bounds for axis 1 with size 377 i = 372 index 384 is out of bounds for axis 1 with size 377 i = 373 index 385 is out of bounds for axis 1 with size 377 i = 374 index 386 is out of bounds for axis 1 with size 377 i = 375 index 387 is out of bounds for axis 1 with size 377 i = 376 index 388 is out of bounds for axis 1 with size 377