为什么说索引越界了?萌新求助
本帖最后由 勇者大人 于 2020-9-24 13:09 编辑题目:生成一个在1- 100范围以内50个数字整数列表,查找列表中相乘等于1008的3个数
【索引越界?求大佬帮忙看看是什么问题】
我的代码:
import random
intList = []
for i in range(50):
intList = random.randint(1,101)
for i in intList:
for j in intList:
for k in intList:
if intList * intList * intList == 1008:
break
报错信息:line 4, in <module>
intList = random.randint(1,101)
IndexError: list assignment index out of range
Process finished with exit code 1 刚开始时intList是空的,intList中不管i是什么数都是越界,我猜你是想intList.append(random.randint(1,101)) 你是想要这样吧?
import random
intList = []
for i in range(50):
intList.append(random.randint(1,101))
print(intList)
for i in range(0,len(intList)):
for j in range(i,len(intList)):
for k in range(j,len(intList)):
if intList * intList * intList == 1008:
print(intList ,intList , intList) hrp 发表于 2020-9-24 13:12
刚开始时intList是空的,intList中不管i是什么数都是越界,我猜你是想intList.append(random.randint(1,101 ...
谢谢大佬 解决了{:10_254:} 疾风怪盗 发表于 2020-9-24 13:15
你是想要这样吧?
嗯嗯 修改了一下
import random
intList = random.sample(range(1,101),50)
print(intList)
for i in range(0,len(intList)):
for j in range(i+1,len(intList)):
for k in range(j+1,len(intList)):
if intList * intList * intList == 1008:
print(intList ,intList , intList)
页:
[1]