我不能使用ListName.insert()函数,它说ListIndex错误

时间:2016-03-22 06:17:45

标签: python python-3.x

我不是计算机科学或计算机相关专业。我是土木工程专业的学生,​​我正在尝试制作应用程序以简化无法通过电子表格计算的计算。我知道python基本的。请不要对我残忍哈哈

所以我有这个代码

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

我的问题是,如果我没有添加此代码,代码可以完美无瑕地运行

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

这些代码实际上类似于下面的代码

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

但是当程序运行此代码时

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

它总是有ListIndex错误

这是追溯

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

请帮助我,我已经被困在这段代码中两天了。 感谢

2 个答案:

答案 0 :(得分:1)

您需要为使用conus[y+a1]的所有while语句添加条件,因为y + a1大于conus的长度。

所以......看起来像是:

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

这会检查长度conus是否小于y + a1。例如。如果conus = [[0.0, 3.0], [1.0, 4.0], [2.0, 5.0]] len(conus)为3.您获得IndexError的位置,是因为y +a13,而conus is Zer0 indexed that would mean that是圆锥[2} ] == [2.0,5.0] . in other words conus [3]`会失败。

答案 1 :(得分:1)

在此迭代中,您将填写列表:

for i in range(0, jumlahTitik):

表示您的最高索引为jumlahTitik - 1

在此次迭代中,您尝试访问列表:

while y <= jumlahTitik:

在您的上一次迭代y jumlahTitika1将> 0

所以conus[y+a1]尝试访问无效的conus[jumlahTitik + a1],因为您的最高索引为jumlahTitik - 1