简体   繁体   中英

logical error in Python Lottery Programme

I am writing a code in which the user enters 6 'lottery' numbers between 1 and 59 - that bit works fine. Then the computer picks 6 random numbers between 1 and 59-that bit also works well. However then the programme has to compare which numbers in both arrays match up and it has to output 'you have (number) matches', at the moment it's matching some numbers but there is a logical error somewhere.

In an attempt to figure out what is happening I tried changing the programme so the computer picks 6 numbers which are between 1 and 6, so every array it comes up with has the exact same numbers. I entered the user's number's as 1 to 6, so technically the programme should have outputted 'you have 6 matches', but instead it outputted 'you have 4 matches' for some of them 3 matches for some, and 2 for some.

    import random 
    lotterynumbers = []
    usernumbers = []
    compnum = 0
    usernum = 0
    matches = 0

    #user picks numbers 
    while len(usernumbers) < 6:
        try:
            usernum = int(input("input a number between 1 and 59 "))
        except ValueError: #if the input is not an integer
        print("your input must be a number between 1 and 59 ")
        continue
    if usernum > 59 or usernum < 1:
        print("your number has to be between 1 and 59 ")
        continue
    elif usernum in usernumbers: #if the input has already been entered into the array
        print("you have already used this number")
        continue  
    else:
        usernumbers.append(usernum)

    timesplay = int(input("how many times would you like to play the lottery? "))
    timesplayed = 0

    while timesplayed < timesplay:
        counter = 0
        while len(lotterynumbers) < 6: #means the while loop runs whilst the array has less than 6 values
            compnum = random.randint(1,59) #imports a random number between 1 and 59
            while compnum in lotterynumbers:
                compnum = random.randint(1,59)
            lotterynumbers.append(compnum) #adds numbers to the array

            if usernumbers[counter] in lotterynumbers: 
                matches = matches + 1 #check computer numbers against the user numbers
            counter = counter + 1
        print (lotterynumbers)
        print ("you have ",matches,"matches")
        lotterynumbers = [] #re-sets the array so the computer can find a new list of numbers 
        matches = 0 #re-sets matches to 0 for each draw of the lottery   
        timesplayed = timesplayed + 1  

I think the problem is with these lines:

if usernumbers[counter] in lotterynumbers: 
    matches = matches + 1 #check computer numbers against the user numbers

Since this is in the loop where the computer numbers are generated, the early user numbers are only checked against the early computer numbers. Try replacing with:

if compnum in usernumbers:
    matches = matches + 1

ie, "is the number that the computer chose anywhere in the list of user numbers".

The problem is that you check the user's numbers before the computer has picked all six numbers. For instance, when you check usernumbers[2], the computer has picked only 3 numbers so far; if the user's pick matches one of the later numbers, you'll miss it.

Use two separate loops:

for count in range(6):
    compnum = random.randint(1,59) # choose a number between 1 and 59
        while compnum in lotterynumbers:
            compnum = random.randint(1,59)
        lotterynumbers.append(compnum)

matches = 0
for count in range(6):
    if usernumbers[count] in lotterynumbers:
        matches += 1

There are a lot of things you can do better as you learn more Python, but this should get you over the immediate problem.


PYTHONIC METHOD

  1. Use random.sample to grab six numbers from a population you supply.
  2. range(1,60) gives you the integers 1 through 59.
  3. To count the matches, make all six checks at once, and then add up the True results.

Result:

computer_numbers = random.sample(range(1,60), 6)
matches = sum([user_numbers[i] in computer_numbers for i in range(6)])

Once you input all six user numbers, you can do the lottery draw and check like that. Note that you can collapse this to a single line, if you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM