我在尝试运行此程序时收到IndexError:list assignment index超出范围错误。我的索引看起来很好(0到8),我不认为.append是必需的,因为等号分配每次传递的随机值。我错过了什么?
import random
#The main function.
def main():
#Welcome message.
print("Welcome to the lottery number generator program!")
print()
#Explain what the program does.
print("Note: This program will randomly generate a 7 digit lottery number and display it to the screen. ")
print("________________________________________________________________________________________________")
print()
print()
#Call the generateNumbers function and store its returned list in variable lotteryNumbers.
lotteryNumbers = generateNumbers()
#Call the printLottery function and pass the lotteryNumbers list as argument.
printLottery(lotteryNumbers)
#The generateNumbers function generated 7 random digits between 0 and 9 stores them in a list and returns the list.
def generateNumbers():
#A list variable to hold empty list.
lotteryNumbers = []
#Declare and set loop counter to 0.
index = 0
for index in range (0,8):
lotteryNumbers[index] = random.randrange(0,10)
index += 1
return lotteryNumbers
def printLottery(lotteryNumbers):
print("Here are the 7 lucky numbers: {}".format(lotteryNumbers))
#End main
main()
答案 0 :(得分:3)
列表与其他语言的数组不同!
lotteryNumbers
初始化为空列表。其中没有任何内容。它的长度为零。您需要将random.randrange(0, 10)
添加到空列表中。这是通过.append()
答案 1 :(得分:3)
for index in range (0,8):
lotteryNumbers[index] = random.randrange(0,10)
index += 1
这不符合您的希望。您不能将值分配给列表中当前不存在的位置,并且该列表当前为空,这意味着您根本无法执行此操作。
你想要的是:
for index in range (0,8):
lotteryNumbers.append(random.randrange(0,10))
您不需要index += 1
,因为python会在for
循环中为您处理此问题。
https://docs.python.org/2/library/random.html#random.sample
例如:
lotteryNumbers = random.sample(xrange(10), 7)
虽然彩票中有超过10个选项也是正常的!
答案 2 :(得分:0)
您应该append
新值:
def generateNumbers():
#A list variable to hold empty list.
lotteryNumbers = []
#Declare and set loop counter to 0.
index = 0
for _ in range (0,8):
lotteryNumbers.append(random.randrange(0,10))
return lotteryNumbers
或将列表构建为您想要的大小:
def generateNumbers():
#A list variable to hold empty list.
lotteryNumbers = [0]*8
#Declare and set loop counter to 0.
index = 0
for index in range (0,8):
lotteryNumbers[index] = random.randrange(0,10)
return lotteryNumbers
另请注意,您不必增加索引,您已经在范围内进行迭代。
答案 3 :(得分:0)
您正尝试访问列表中不存在的元素。
要构建列表,您可以使用list.append()
lotteryNumbers = []
for _ in range(8):
lotteryNumbers.append(random.randrange(0,10))
或者,正如它在Python中常见的那样,使用list comprehension:
lotteryNumbers = [random.randrange(0,10) for _ in range(8)]
通常更有效,更简洁。
答案 4 :(得分:0)
我认为你尝试将元素添加到数组中,如
lotteryNumbers = [0,0,0,0,0,0,0]
答案 5 :(得分:0)
通过
初始化列表let person: Person[] = [["Anne", 21], ["Coco", 33]];
class Person{
person: Person[];
printPerson(): person{
for (let i of person) {
document.write( i + '<br/>');
}
}
它正好有0个元素。但是
lotteryNumbers = []
您尝试访问列表的第1个,第2个,...,第n个元素。您的代码不会将元素插入列表。为了避免这种情况,有几种方法。
lotteryNumbers[index] = random.randrange(0,10)
lotteryNumbers = {}
lotteryNumbers = [0,0,0,0,0,0,0,0]
lotteryNumbers = list(range(8))
答案 6 :(得分:0)
这很有效。使用您想要的数字创建一个数组,并随机选择一个项目。然后,删除该项并减少一个长度(使用pop)。在这个例子中,数字1到7
lotteryNumbers = []
rij = []
for i in range(aantal):
rij.append(i)
for j in range(7):
r = random.randrange(0,7-j)
k = rij.pop(r)
lotteryNumbers.append(k+1)
print(lotteryNumbers)