Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (11.4k points)

I think I have my program completed, but... it doesn't work. I'm trying to write a program that simulates a lottery game, but when I try to check the user's guesses against the number of guesses on the ticket, I get an error that tells me the "list index is out of range". I think it has something to do with the part of the code where I assign the random digits to "a," "b", "c", etc. But I'm not sure.

Here is the code in its entirety:

import random

def main():

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

    b = random.randint(1,30)
    while not (b in winning_numbers):

    c = random.randint(1,30)
    while not (c in winning_numbers):

    d = random.randint(1,30)
    while not (d in winning_numbers):

    e = random.randint(1,30)
    while not (e in 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(" ")

#Checks the user's 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


And here is the error I get:

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

1 Answer

0 votes
by (32.3k points)

I think the errors that you are getting are based on the input statement, bubble is a list of strings. This list then gets appended to guess. So guess would look something like: [['12', '30', '12', '23'. '3'], ['4','5','7','8','16']]. The proper list method is guess.extend(). And since the values are strings, if you do a comparison,  it will never return true.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories