Help with code: Lotto number generator

#Program to select a Lotto numbers for NZ Lotto
import random

#Initiate empty Lotto number list
lotto_numbers = []

#Set index to zero
i = 0
k = 0

#Create list of 10 random numbers ranging from 1 to 40
for i in range(10):
    x = str(random.randint(1,40))
    lotto_numbers.append(x)

#Sort list by grouping duplicates
lotto_numbers.sort()

#Initiate new Lotto number list for removal of duplicates
new_lotto_numbers = []

removal = 0

#Check for duplicatrs and remove
for number in lotto_numbers:
    if number not in new_lotto_numbers:
        new_lotto_numbers.append(number)
        removal = removal + 1

#Calculate number of duplicate elements
deleted_elements = len(lotto_numbers) - removal

j = 0

#Refill list with new non duplicte elements
if deleted_elements > 0:
    for j in range(deleted_elements):
        x = str(random.randint(1,40))
        if x not in new_lotto_numbers:
            new_lotto_numbers.append(x)
            
powerball = str(random.randint(1,10))
    
print("Lotto numbers:",new_lotto_numbers,"\nPowerball:",powerball)

I want to give the user an option to print out n number of lotto numbers and powerball combinations.

I’ll do that by having the user enter number of lines & then loop. A line is a combination of lotto numbers and powerball

k = 0

lines = int(input("Enter number of lines: "))

while k < lines:

#code

But what happens is that the first line is okay but the following lines are appended lotto number lists from the previous line so I get lotto number lists of 10+ elements for each pass through the while loop.

Looking for some assistance.

Thanks.

You will need to reset the list each time around the loop.

randint() might return the same number all 10 times, that is a valid random possiblity?
Have a look at the random docs you may see that there is function that fits you needs better.

Thanks Barry.

I reset the list by placing it in the while loop. Output is fixed.
As for the randint(), there is a line of code that checks for duplicates & then removes them from the list & then refills the list with a new set of numbers (not duplicates).

The only issue I’ve encountered is that when there a 3 or more duplicates within the list, the final output list contains 9 & not 10 elements. Rarely happens but I did pick it up once or twice.

Here is the new line of code:

#Program to select a Lotto numbers for NZ Lotto
import random

#Set index to zero
i = 0
k = 0

lines = int(input("Enter number of lines: "))
#Initiate empty Lotto number list

while k <= lines:
    lotto_numbers = []

    #Create list of 10 random numbers ranging from 1 to 40
    for i in range(10):
        x = str(random.randint(1,40))
        lotto_numbers.append(x)

    #Sort list by grouping duplicates
    lotto_numbers.sort()

    #Initiate new Lotto number list for removal of duplicates
    new_lotto_numbers = []

    removal = 0

    #Check for duplicatrs and remove
    for number in lotto_numbers:
        if number not in new_lotto_numbers:
            new_lotto_numbers.append(number)
            removal = removal + 1

    #Calculate number of duplicate elements
    deleted_elements = len(lotto_numbers) - removal

    j = 0

    #Refill list with new non duplicte elements
    if deleted_elements > 0:
        for j in range(deleted_elements):
            x = str(random.randint(1,40))
            if x not in new_lotto_numbers:
                new_lotto_numbers.append(x)
            
    powerball = str(random.randint(1,10))
    
    print("Lotto numbers:",new_lotto_numbers,"\nPowerball:",powerball)

    k = k + 1

Took your advice & found a better number generator function.
Much better without the list bug when duplicates >= 3.
In fact I don’t think that the function creates any duplicates.

#Program to select a Lotto numbers for NZ Lotto
import random
import numpy as np

#Set index to zero
i = 0
k = 0

lines = int(input("Enter number of lines: "))

while k <= lines:

    #Create list of 10 random numbers ranging from 1 to 40
    lotto_numbers = np.random.choice(range(1, 41), size=10, replace=False)
    
    lotto_numbers.sort()
            
    #Calculate powerball number
    powerball = str(random.randint(1,10))
    
    print("Lotto numbers:",lotto_numbers,"\nPowerball:",powerball)
    
    #increment loop counter
    k = k + 1


I was thinking of random — Generate pseudo-random numbers — Python 3.13.2 documentation that will give you exactly what you want.

FYI there is another module that can be used for random numbers called secrets. It is for use cases where you need high quality random numbers, like cryptography.