Python: IndexError: Out-of-range index index

I think my program is complete, but ... it does not work. I am trying to write a program that simulates a lottery game, but when I try to verify that the user is guessing the number of guesses on the ticket, I get an error message that tells me that "the list index is out of range." I think this has something to do with the part of the code where I assign the random digits "a", "b", "c", etc. But I'm not sure.

Here is the complete code:

import random def main(): random.seed() #Prompts the user to enter the number of tickets they wish to play. tickets = int(input("How many lottery tickets do you want?\n")) #Creates the dictionaries "winning_numbers" and "guess." Also creates the variable "winnings" for total amount of money won. winning_numbers = [] guess = [] winnings = 0 #Generates the winning lotto numbers. for i in range(tickets): del winning_numbers[:] a = random.randint(1,30) while not (a in winning_numbers): winning_numbers.append(a) b = random.randint(1,30) while not (b in winning_numbers): winning_numbers.append(b) c = random.randint(1,30) while not (c in winning_numbers): winning_numbers.append(c) d = random.randint(1,30) while not (d in winning_numbers): winning_numbers.append(d) e = random.randint(1,30) while not (e in winning_numbers): winning_numbers.append(e) print(winning_numbers) getguess(guess, tickets) nummatches = checkmatch(winning_numbers, guess) print("Ticket #"+str(i+1)+": The winning combination was",winning_numbers,".You matched",nummatches,"number(s).\n") if nummatches == 0 or nummatches == 1: winnings = winnings + 0 elif nummatches == 2: winnings = winnings + 10 elif nummatches == 3: winnings = winnings + 500 elif nummatches == 4: winnings = winnings + 20000 elif nummatches == 5: winnings = winnings + 1000000 print("You won a total of",winnings,"with",tickets,"tickets.\n") #Gets the guess from the user. def getguess(guess, tickets): del guess[:] for i in range(tickets): bubble = input("What numbers do you want to choose for ticket #"+str(i+1)+"?\n").split(" ") guess.append(bubble) print(bubble) #Checks the user guesses with the winning numbers. def checkmatch(winning_numbers, guess): match = 0 for i in range(5): if guess[i] == winning_numbers[i]: match = match+1 return match main() 

And here is the error I get:

 Traceback (most recent call last): File "C:\Users\Ryan\Downloads\Program # 2\Program # 2\lottery.py", line 85, in <module> main() File "C:\Users\Ryan\Downloads\Program # 2\Program # 2\lottery.py", line 45, in main nummatches = checkmatch(winning_numbers, guess) File "C:\Users\Ryan\Downloads\Program # 2\Program # 2\lottery.py", line 79, in checkmatch if guess[i] == winning_numbers[i]: IndexError: list index out of range 
+6
source share
3 answers

As the error notes, the problem is on the line:

 if guess[i] == winning_numbers[i] 

The error is that the indices of your list are out of range - i.e. You are trying to access some index that does not even exist. If you are not completely debugging your code, I would check the line where you add guesses based on input:

 for i in range(tickets): bubble = input("What numbers do you want to choose for ticket #"+str(i+1)+"?\n").split(" ") guess.append(bubble) print(bubble) 

The size of the number of guesses provided by your user is based on

 # Prompts the user to enter the number of tickets they wish to play. tickets = int(input("How many lottery tickets do you want?\n")) 

So, if the number of tickets they want is less than 5, then your code is here

 for i in range(5): if guess[i] == winning_numbers[i]: match = match+1 return match 

throws an error because there are simply not many elements in the guess list.

+7
source

I think you want to put the casting of random a, b, c, etc. inside the loop:

 a = None # initialise while not (a in winning_numbers): # keep rolling an a until you get one not in winning_numbers a = random.randint(1,30) winning_numbers.append(a) 

Otherwise, a will be generated only once, and if it is already in winning_numbers , it will not be added. Since the generation of a is outside the while (in your code), if a already in winning_numbers , then it is too bad, it will not be redeployed, and you will have one less winning number.

This may be the cause of your error in if guess[i] == winning_numbers[i] . (Your winning_numbers do not always have a length of 5).

0
source

Here is your code. I assume you are using python 3 based on the use of print() and input() :

 import random def main(): #random.seed() --> don't need random.seed() #Prompts the user to enter the number of tickets they wish to play. #python 3 version: tickets = int(input("How many lottery tickets do you want?\n")) #Creates the dictionaries "winning_numbers" and "guess." Also creates the variable "winnings" for total amount of money won. winning_numbers = [] winnings = 0 #Generates the winning lotto numbers. for i in range(tickets * 5): #del winning_numbers[:] what is this line for? randNum = random.randint(1,30) while randNum in winning_numbers: randNum = random.randint(1,30) winning_numbers.append(randNum) print(winning_numbers) guess = getguess(tickets) nummatches = checkmatch(winning_numbers, guess) print("Ticket #"+str(i+1)+": The winning combination was",winning_numbers,".You matched",nummatches,"number(s).\n") winningRanks = [0, 0, 10, 500, 20000, 1000000] winnings = sum(winningRanks[:nummatches + 1]) print("You won a total of",winnings,"with",tickets,"tickets.\n") #Gets the guess from the user. def getguess(tickets): guess = [] for i in range(tickets): bubble = [int(i) for i in input("What numbers do you want to choose for ticket #"+str(i+1)+"?\n").split()] guess.extend(bubble) print(bubble) return guess #Checks the user guesses with the winning numbers. def checkmatch(winning_numbers, guess): match = 0 for i in range(5): if guess[i] == winning_numbers[i]: match += 1 return match main() 
0
source

Source: https://habr.com/ru/post/908301/


All Articles