【发布时间】:2016-10-19 19:06:09
【问题描述】:
我需要生成一个随机数 1 - 3。我得到的错误是
IndexError: 列表索引超出范围
我的代码如下:
weaponList = [0,1,2]
weapon2 = weaponList[random.randint(0,3)]
【问题讨论】:
-
randint包含在内,你要(0,2)。 -
或
random.randrange(len(weaponList))或只是weapon2 = random.choice(weaponList) -
谢谢@MorganThrapp,这就是我想要的。
标签: python list python-3.x random int