索引器错误:while循环中的列表索引超出范围

2024-04-26 09:23:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我不明白为什么索引出错了。在

a = array1[item]

    while item <= n:
        a = array1[item]
        t = mid - a
        l = h_f(t)
        ab += l
        if ab > k:
            item+=1
            break
        ae += h_f(t + 1) - 1
        item+=1

n—输入值的个数,项=0

^{pr2}$

有什么帮助吗?在


Tags: ifabitembreakmidwhile个数ae
2条回答

数组从0开始索引,因此最后一个索引为2。 当n可以是3时,代码可以越界

while item < n:

具有n元素的列表在索引0n-1中有项。一、 例如,循环应该在n之前停止,而不是在它之前。将<=替换为<,您应该可以:

while item < n:
   # Here -^

相关问题 更多 >