2017-07-25 73 views
0

我試圖運行該程序時,出現IndexError:list assignment index超出範圍錯誤。我的索引看起來很好(從0到8),我不認爲.append是需要的,因爲等號每次傳遞隨機值。我錯過了什麼?在python中生成隨機整數列表3

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() 
+1

您不能分配一個列表dinamically,你需要預先分配,如果你w螞蟻通過切片來訪問它的元素。你需要使用append – gionni

+0

'index + = 1'不是必需的。相反: – fredtantini

+1

:追加是需要的! – dnalow

回答

0

添加元素陣列通過初始化列表與

lotteryNumbers = [] 

它正好有0個元素。但與

lotteryNumbers[index] = random.randrange(0,10) 

您嘗試訪問列表的第1,第2,..,第n個元素。您的代碼不會將元素插入列表中。爲了避免這種情況,有許多方法。

  • 創建一個字典,而不是一個列表。一個字典實際上創建了不存在的元素:lotteryNumbers = {}
  • 預初始化列表中有8個要素:
    • lotteryNumbers = [0,0,0,0,0,0,0,0]
    • lotteryNumbers = list(range(8))
  • 但最最好的變體應使用附加: lotteryNumbers.append(random.randrange(0,10))
3

列表不像其他語言的數組!

lotteryNumbers被初始化爲空列表。沒有什麼。它的長度是零。您需要將random.randrange(0, 10)添加到空白列表中。這是通過.append()

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
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)) 

爲你詮釋的for循環不需要index += 1因爲Python處理此。


順便說一句,彩票一般都是挑選不換,所以難道你真的不想抽樣嗎?

https://docs.python.org/2/library/random.html#random.sample

如:

lotteryNumbers = random.sample(xrange(10), 7) 

雖然這也是正常的彩票有遠遠超過10個選項!

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)] 

通常是更有效簡潔。

0

我認爲你嘗試像

lotteryNumbers = [0,0,0,0,0,0,0]