FDuldul
FDuldul

Reputation: 167

i can't use ListName.insert() function it said ListIndex error

i'm not in computer science or computer related major. i'm civil engineering student, and i'm trying to make app to ease my calculation that can't be calculated by spreadsheet. i know python basic. and please don't be cruel to me haha

so i have this code

from math import ceil

print('Masukan jarak antar pias')
jarakAntarPias = float(input())
print('Masukan kedalaman sondir')
kedalamanSondir = float(input())

jumlahTitik = int(ceil(kedalamanSondir/jarakAntarPias+1))
conus = []

cn2a = []
cn3a = []

for i in range(0, jumlahTitik):
    a = float(input())
    if a < 0:
        b = float(input())
        conus[i-1] = [(i-1)*jarakAntarPias, b]
        a = float(input())
    if a >= 0:
        conus.insert(i, [i*jarakAntarPias, a])


conusAtas = []
conusBawah = []

print(conus)
diameter = float(input())
d4 = diameter*4
d8 = diameter*8

y = 0
a1 = 0
while y <= jumlahTitik:
    if (d4/jarakAntarPias+1) >= y:
        while a1 <= y:
            conusAtas.insert(a1, conus[y-a1][1])
            a1 += 1
    else:
        while a1 <= (int((d4/jarakAntarPias))+1):
            conusAtas.insert(a1, conus[int((d4/jarakAntarPias))+1-a1][1])
            a1 += 1
    a1 = 0
    print(conusAtas)

    if (d8/jarakAntarPias+1) <= jumlahTitik-y:
        while a1 <= jumlahTitik-y:
            conusBawah.insert(a1, conus[y+a1][1])
            a1 += 1
    else:
        while a1 <= (d8/jarakAntarPias+1):
            conusBawah.insert(a1, conus[y+a1][1])
            a1 += 1
    #more code below
    print(conusBawah)
    conusAtas = []
    conusBawah = []
    y += 1

my problem is, the code works flawlessly as i expected if i didn't add this code

if (d8/jarakAntarPias+1) <= jumlahTitik-y:
    while a1 <= jumlahTitik-y:
        conusBawah.insert(a1, conus[y+a1][1])
        a1 += 1
else:
    while a1 <= (d8/jarakAntarPias+1):
        conusBawah.insert(a1, conus[y+a1][1])
        a1 += 1

those code actually similar like this code below

if (d4/jarakAntarPias+1) >= y:
    while a1 <= y:
        conusAtas.insert(a1, conus[y-a1][1])
        a1 += 1
else:
    while a1 <= (int((d4/jarakAntarPias))+1):
        conusAtas.insert(a1, conus[int((d4/jarakAntarPias))+1-a1][1])
        a1 += 1

but when the program is running this code

conusBawah.insert(a1, conus[y+a1][1])

it always have ListIndex error

this is the traceback

Traceback (most recent call last):
File "C:/Users/afahm/PycharmProjects/untitled2/DULS.py", line 56, in <module>
conusBawah.insert(a1, conus[y+a1][1])
IndexError: list index out of range

please help me, i've been stuck in this code for two days. thanks

Upvotes: 1

Views: 51

Answers (2)

Renier
Renier

Reputation: 1535

you need to add a condition to all of your while statements where you use conus[y+a1], because y + a1 is bigger that the length of conus.

so... it would look like:

if (d4/jarakAntarPias+1) >= y:
    while a1 <= y and (y+a1) < len(conus):
        conusAtas.insert(a1, conus[y-a1][1])
        a1 += 1
else:
    while a1 <= (int((d4/jarakAntarPias))+1) and (int((d4/jarakAntarPias))+1-a1) < len(conus):
        conusAtas.insert(a1, conus[int((d4/jarakAntarPias))+1-a1][1])
        a1 += 1
a1 = 0
print(conusAtas)

if (d8/jarakAntarPias+1) <= jumlahTitik-y:
    while a1 <= jumlahTitik-y and (y+a1) < len(conus):
        conusBawah.insert(a1, conus[y+a1][1])
        a1 += 1
else:
    while a1 <= (d8/jarakAntarPias+1) and (y+a1) < len(conus):
        conusBawah.insert(a1, conus[y+a1][1])
        a1 += 1

This checks that the length conus is less than y + a1. eg. if conus = [[0.0, 3.0], [1.0, 4.0], [2.0, 5.0]] the len(conus) would be 3. you where getting the IndexError, is because y +a1 was 3, and because conus is Zer0 indexed that would mean thatconus[2] == [2.0, 5.0]. in other wordsconus[3]` would fail.

Upvotes: 1

TobiasR.
TobiasR.

Reputation: 801

In this iteration you are filling your list:

for i in range(0, jumlahTitik):

which means, your highest index is jumlahTitik - 1

in this iteration you are trying to access your list:

while y <= jumlahTitik:

On your last iterarion y would be jumlahTitik and a1 would be >0

so conus[y+a1] trys to access conus[jumlahTitik + a1] which cant work, since your highest index is jumlahTitik - 1

Upvotes: 1

Related Questions