I have a question for Python's novice.

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

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

When I bought n lotto tickets, I coded it with Python

The answer is [1,2,3,4,5,6,7] [2,3,4,5,6,7,8] It doesn't come out separately like this, but it comes out continuously like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].

How can I print it out separately?

python

2022-09-20 11:17

1 Answers

I think it's because I kept putting numbers in one list lotto_num.

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


2022-09-20 11:17

If you have any answers or tips


© 2022 pinfo. All rights reserved.