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
#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


Messages In This Thread
RE: I have an index error inline 76 but I write the program in a way that cant reach tha - by buran - Nov-13-2020, 07:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyscript index error while calling input from html form pyscript_dude 2 938 May-21-2023, 08:17 AM
Last Post: snippsat
  Index error help MRsquared 1 739 May-15-2023, 03:28 PM
Last Post: buran
  Program running on RPi 3b+ Very Strange Behavior - Out of Bound Index MadMacks 26 3,053 Mar-07-2023, 09:50 PM
Last Post: MadMacks
  how to write exception error into logger mg24 3 948 Nov-15-2022, 04:20 PM
Last Post: insharazzak
  Take inline argument value SM_13 2 1,073 Jul-06-2022, 11:12 AM
Last Post: snippsat
  I'm getting a String index out of range error debian77 7 2,280 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  Python Error List Index Out of Range abhi1vaishnav 3 2,239 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  Index error - columns vs non-column Vinny 3 4,848 Aug-09-2021, 04:46 PM
Last Post: snippsat
  write a program which prints the day in english ben1122 10 3,913 Jul-25-2021, 05:55 PM
Last Post: ben1122
  How to resolve Index Error in my code? codify110 6 2,956 May-22-2021, 11:04 AM
Last Post: supuflounder

Forum Jump:

User Panel Messages

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