【问题标题】:IndexError: list index out of range I don't understandIndexError:列表索引超出范围我不明白
【发布时间】:2016-10-20 14:07:47
【问题描述】:

这个程序应该把所有可以被 3 和 5 整除的数字加在一起,但我不明白,我尝试运行这个脚本,它总是给我错误:

Traceback (most recent call last):
File "first.py", line 23, in <module>
main()
File "first.py", line 19, in main
merica = merica + good[count-1]
IndexError: list index out of range

我不明白这是什么意思,因为

count-1 

似乎在索引范围内吗?

def main():
    merica = 0
    commonfactors = []
    good = []
    count = 1

    while count <= 1000:
            if count%3 == 0:
                    good.append(count)
            elif count%5 == 0:
                    if count in good:
                            commonfactors.append(count)
                    else:
                            good.append(count)
            count = count+1
    count = count - 1000
    while count <= 1000:
            merica = merica + good[count-1]
            count = count+1
    print(merica)
main()
exit()

任何帮助将非常感谢!

【问题讨论】:

  • 您是否在出现错误的地方打印了count-1
  • 还有len(good)?你正在做一个假设。

标签: python list indexing


【解决方案1】:

只有当计数是 3 或 5(不是两者)的倍数时,才将 number 附加到 good。

所以,good 的大小不会是 1000。

这就是你得到错误的原因。

您的以下循环将只运行到 count

def main():
    merica = 0
    commonfactors = []
    good = []
    count = 1

    while count <= 1000:
            if count%3 == 0:
                    good.append(count)
            elif count%5 == 0:
                    if count in good:
                            commonfactors.append(count)
                    else:
                            good.append(count)
            count = count+1
    count = count - 1000
    while count <= len(good):
            merica = merica + good[count-1]
            count = count+1
    print(merica)
main()
exit()

【讨论】:

    【解决方案2】:

    由于错误表明列表索引超出范围,让我们考虑一下列表有多长以及我们尝试使用什么索引。在这里,假设列表 good 中有 1000 个项目,因为 while 循环上升到 1000 个。

    while count <= 1000:
            merica = merica + good[count-1]
            count = count+1
    

    好的,现在我们看到程序希望列表中有 1000 个项目。让我们在构建列表时看看这是否成立。

    while count <= 1000:
            if count%3 == 0:
                    good.append(count)
            elif count%5 == 0:
                    if count in good:
                            commonfactors.append(count)
                    else:
                            good.append(count)
            count = count+1
    

    现在我们看到列表是通过附加值来构造的,其中值从 1 到 1000。但是我们应用了一个条件来附加它,即它必须可以被 3 或 5 整除。那么关于素数?没有附加这些数字的else 块。因此,我们不会附加完整的 1000 个数字,而只会附加可被 5 或 3 整除的数字。

    解决方案是改变我们对第二个 while 块中循环大小的假设。 while count &lt;= len(good): 会根据列表的大小调整长度。

    【讨论】:

      【解决方案3】:

      整个块都假设good 的长度不准确。

      while count <= 1000:
              merica = merica + good[count-1]
              count = count+1
      

      如果您只在其中放置数字的 子集,则长度不能为 1000。

      无论如何,你应该了解一下内置的sum()函数:

      good = [1, 2, 3]
      merica = sum(good)
      

      您可能还会发现range() 函数也很有帮助:

      for number in range(1000):
          # This will loop 1000 times with number equal to 0 to 999.
      

      这变成了代码审查......但是你为什么要这样做:

      count = count - 1000
      

      而不仅仅是设置count = 0?

      您还可以从一些更具防御性的编程中受益。了解如何使用assert

      count = count - 1000
      assert count == 0
      

      assert count-1 < len(good)
      merica = merica + good[count-1]
      

      这些断言将帮助您快速追踪您所做的假设。

      祝你好运!

      【讨论】:

        猜你喜欢
        • 2011-10-31
        • 2015-06-26
        • 1970-01-01
        • 1970-01-01
        • 2017-11-11
        相关资源
        最近更新 更多