Python Forum
I have an index error inline 76 but I write the program in a way that cant reach tha
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I have an index error inline 76 but I write the program in a way that cant reach tha
#1
##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
Reply
#2
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.
buran and abbaszandi like this post
Reply
#3
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to resolve Index Error in my code? codify110 6 367 May-22-2021, 11:04 AM
Last Post: supuflounder
  index error surim 4 537 Dec-05-2020, 02:34 PM
Last Post: deanhystad
  when this error rise?index 28 is out of bounds for axis 0 with size 13 abbaszandi 1 607 Nov-10-2020, 08:46 PM
Last Post: deanhystad
  List index out of range error while accessing 2 lists in python K11 2 591 Sep-29-2020, 05:24 AM
Last Post: K11
  List index out of range error when attempting to make a basic shift code djwilson0495 4 811 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  program error Dalpi 4 796 Apr-30-2020, 11:46 PM
Last Post: Dalpi
  IndexError: index 0 is out of bounds for axis 0 with size 0 error tmhsa 5 1,442 Apr-25-2020, 01:15 PM
Last Post: tmhsa
  index error in text script Bartry 2 854 Dec-09-2019, 11:50 AM
Last Post: Malt
  How to write a script to execute a program need passing additional input? larkypython 2 970 Nov-23-2019, 04:38 AM
Last Post: larkypython
  ValueError: Index contains duplicate entries, cannot reshapeā€¯ error when I try to use Smiling29 11 4,581 Oct-26-2019, 09:52 PM
Last Post: Smiling29

Forum Jump:

User Panel Messages

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