-1
from random import *
lotto_num = []
for j in range(7) :
    for i in range(1, 8) :
        number = randint(1, 46)
        while number in lotto_num:
            number = randint(1, 46)
        lotto_num.append(number)
        lotto_num.sort()
    print("{0}".format(lotto_num))

This code makes lottery number so, I want to get lists like

[1,2,3,4,5,6,7]
[2,3,4,5,6,7,8]
[3,4,5,6,7,8,9]
...

but my code only has shown like this

[1,2,3,4,5,6,7,8,9,10.......] ## <- just add number without make new lists!

how i can fix this?

pypypy
  • 11
  • 1
  • Related: [Python pick 20 random results from list](https://stackoverflow.com/questions/37609091/python-pick-20-random-results-from-list), [Select 50 items from list at random to write to file](https://stackoverflow.com/questions/15511349/select-50-items-from-list-at-random-to-write-to-file) - there are others. – wwii Jan 27 '22 at 15:00
  • Also: [How to get all combinations of length n in python](https://stackoverflow.com/questions/27974126/how-to-get-all-combinations-of-length-n-in-python), [Algorithm to return all combinations of k elements from n](https://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n) – wwii Jan 27 '22 at 15:05

3 Answers3

2

You continually append to lotto_num without resetting it. So it needs to be reset after each print in order to make a new number.

Just move your lotto_num definition to inside the first for loop:

from random import *
for j in range(7) :
    lotto_num = []
    for i in range(1, 8) :
        number = randint(1, 46)
        while number in lotto_num:
            number = randint(1, 46)
        lotto_num.append(number)
        lotto_num.sort()
    print("{0}".format(lotto_num))

Output:

[7, 17, 29, 32, 34, 44, 45]
[9, 16, 22, 34, 36, 41, 43]
[5, 10, 14, 22, 23, 34, 46]
[2, 11, 18, 27, 32, 35, 37]
[8, 9, 25, 26, 37, 39, 40] 
[3, 5, 8, 14, 19, 22, 33]  
[4, 5, 14, 17, 18, 30, 34]
Eli Harold
  • 2,280
  • 1
  • 3
  • 22
1

First of all, it is recommended to import specific functions instead of whole package. Since we need only randint we can import:

from random import randint

Than your problem can be re-worded as generating n lists of given length l, filled by random numbers from range 1-46.

from random import randint

# number of lotto
n = 5
# length of each lotto
l = 7

# result
lotto_numbers = []

# I am using _ since we do not need that value
for _ in range(n):
    lotto = []
    for _ in range(l):
        number = randint(1, 46)

        while number in lotto:
            number = randint(1, 46)
            
        lotto.append(number)
    
    lotto.sort()
    lotto_numbers.append(lotto)

print(lotto_numbers)

Later you can print generated lotto (or save them into file), that is better approach than printing each lotto right after generation.

...

for lotto in lotto_numbers:
    print(lotto)

Probably the cleanest Pythonic solution which uses random.sample to simulate n times drawing random non-repeated numbers from range 1-46 follows:

from random import sample

# number of lotto
n = 5
# length of each lotto
l = 7

lotto_numbers = [sorted(sample(range(1, 46), l)) for _ in range(n)]

print(lotto_numbers)
mikulatomas
  • 330
  • 1
  • 12
0
from random import *
array=[]
for j in range(7) :
    lotto_num = []
    for i in range(1, 8) :
        number = randint(1, 46)
        while number in lotto_num:
            number = randint(1, 46)
        lotto_num.append(number)
        lotto_num.sort()
    array.append(lotto_num)
    print("{0}".format(lotto_num))