Search code examples
pythoncounter

Implementing a match counter into a lotto number guesser


im rather new to python and found someones lottery simulation in github. After playing around with it for a while i wanted to add a counter, that counts the number of matches of your Number out of the total draws.

I don't know if it is because i did not write the code myself, but i can't seem to make it happen. I've tried some of pythons counter modules bu that did'nt seem to be the right thing.

Heres my code:

import random
import time

### TODO - Refactor GetDrawNumbers (add timers)
### TODO - Refactor CheckNumbers

def GetDrawNumbers():
    drawNumbers = []
    for i in range(6):
        x = None
        while (x == None or x in drawNumbers):
            x = random.randint(1, 49)
        drawNumbers.append(x)
    return drawNumbers

def CheckNumbers(myTicket, actualNumbers):
    numbersMatched = 0
    for number in myTicket:
        if number in actualNumbers:
            numbersMatched += 1
    return numbersMatched

### Script starts here

startTime = time.perf_counter()

myNumbers = [4, 8, 15, 16, 23, 42]

for draw in range(2000):
    drawNumber = draw + 1
    thisWeeksDraw = GetDrawNumbers()
    numbersMatched = CheckNumbers(myNumbers, thisWeeksDraw)
    ##print("Week " + str(drawNumber) + " numbers : " + str(thisWeeksDraw) + " (" + str(numbersMatched) + " matched)")
    if numbersMatched == 4:
        print("Week " + str(drawNumber) + " numbers : " + str(thisWeeksDraw) + " (" + str(numbersMatched) + " matched)")

count = numbersMatched
print("Total matches: " + str(count))

endTime = time.perf_counter()
elapsedTime = endTime - startTime

print("Completed in " + str(elapsedTime) + " seconds!")

If anyone knows a way to implement a counter, that counts the number of times this the program gets 3,4,5 or 6 correct matches i would be super relieved! It's not that this project would be super important but solving the problem would be a milestone for me and my learning process!

Thanks in advance and best wishes!


Solution

  • How about this where I have added a check of the numbersMatched value and increment a counter whenever it is 3 or more

    import random
    import time
    
    ### TODO - Refactor GetDrawNumbers (add timers)
    ### TODO - Refactor CheckNumbers
    
    def GetDrawNumbers():
        drawNumbers = []
        for i in range(6):
            x = None
            while (x == None or x in drawNumbers):
                x = random.randint(1, 49)
            drawNumbers.append(x)
        return drawNumbers
    
    def CheckNumbers(myTicket, actualNumbers):
        numbersMatched = 0
        for number in myTicket:
            if number in actualNumbers:
                numbersMatched += 1
        return numbersMatched
    
    ### Script starts here
    
    startTime = time.perf_counter()
    
    myNumbers = [4, 8, 15, 16, 23, 42]
    
    countOfThreeOrMoreMatched = 0
    for draw in range(2000):
        drawNumber = draw + 1
        thisWeeksDraw = GetDrawNumbers()
        numbersMatched = CheckNumbers(myNumbers, thisWeeksDraw)
        ##print("Week " + str(drawNumber) + " numbers : " + str(thisWeeksDraw) + " (" + str(numbersMatched) + " matched)")
        if numbersMatched >= 3:
            countOfThreeOrMoreMatched += 1
        if numbersMatched == 4:
            print("Week " + str(drawNumber) + " numbers : " + str(thisWeeksDraw) + " (" + str(numbersMatched) + " matched)")
    
    print(f"Count with 3 or more matches {countOfThreeOrMoreMatched}")
    count = numbersMatched
    print("Total matches: " + str(count))
    
    endTime = time.perf_counter()
    elapsedTime = endTime - startTime
    
    print("Completed in " + str(elapsedTime) + " seconds!")