0
This program is to draw numbers for the mega sena. I got the result I wanted, but there are moments that the program stops in the middle or at the beginning of the execution. The user places the amount of guesses he wants. The more problem I’m having is that if the user asks 10 guesses, sometimes runs all, more times than not, and it shows no errors.
from random import randint
from time import sleep
lista = [[]]
num = 0
cont = 0
print('=' * 30)
print(' JOGA NA MEGA SENA')
print('=' * 30)
quantos = int(input('Qual a quantidade de palpites: '))
print(f'===== Sorteando {quantos} Jogos =====')
while quantos != 0:
quantos -= 1
cont += 1
for i in range(0, 6):
num = randint(1, 60)
if num not in lista[0]:
lista[0].append(num)
else:
while num in lista[0]:
num = randint(1, 60)
lista[0].append(num)
if i == 5:
lista[0].sort()
print(f'Jogo {cont}:{lista[0]}')
sleep(1)
for l in range(0, 6):
lista[0].pop()
In case I am using list inside list because of course exercises. If anyone can help me in this I am grateful.
Can you explain the lines 19~21?
while num in lista[0]
– Woss
is that the numbers cannot be repeated, so that while is to q if the number that was randomly generated is generated it generate another and add and only Aira of that loop if another random number is generated
– Gabriel da Silva Santos
this was the way I found to generate another value different from the one inside the list. and then add another
– Gabriel da Silva Santos
But this list grows endlessly and in the end you do
lista[0].pop()
only 6 times. Which means that at some point all the numbers will be on your list and you won’t have any to draw.– Woss
In fact, this is an infinite loop, because its condition
while num in lista[0]
, but you dolista[0].append(num)
, ie, you add the number in the list and then continue the loop if it is in the list. The condition will always be true.– Woss
I understood what Voce wanted to say. But he only enters this condition if he already has that number in there. then in the case he would have to put one . pop() at the end of this condition in specific né?
– Gabriel da Silva Santos
No, because it wouldn’t make sense for you to add it to the list and then remove it from the list. Run a table test and review the condition you’re using.
– Woss
it does not generate an infinite list, because when it has 6 numbers inside the list I print one of these numbers and then delete one by one with a for.
– Gabriel da Silva Santos
All right. I’m gonna have a good look at that code. the problem I’m having msm is that I believe he’s getting lost at some point in the loops
– Gabriel da Silva Santos
When in doubt, test. Add a
print
within this loop and see: https://ideone.com/K6E6uv– Woss