【发布时间】: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)?你正在做一个假设。