Program usually runs but has moments that stops in the middle of execution and does not leave the place

Asked

Viewed 35 times

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]

  • 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

  • this was the way I found to generate another value different from the one inside the list. and then add another

  • 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.

  • In fact, this is an infinite loop, because its condition while num in lista[0], but you do lista[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.

  • 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é?

  • 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.

  • 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.

  • 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

  • When in doubt, test. Add a print within this loop and see: https://ideone.com/K6E6uv

Show 5 more comments

1 answer

1


The problem lies in your logic not to repeat the values:

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)

The idea here is to add the num only if he is not yet in lista[0], however its while will define an infinite loop, since the condition while num in lista[0] will always be true since you always add num in lista[0].

You can simplify this logic by doing:

num = randint(1, 60)
while num in lista[0]:
    num = randint(1, 60)
lista[0].append(0)

That is, draw a value while it is in lista[0] draws a new value. Adds the value in lista[0] only when the loop ends.

Changing that your code will already work.

Alternative

You can use the function random.sample to simplify your code. It will already return a set of single values drawn from your set.

from random import sample

quantos = int(input('Qual a quantidade de palpites: '))

for i in range(quantos):
    print( sorted(sample(range(1, 61), 6)) )
    #                    +----------+  |
    #                          |       |
    #             Conjunto de valores  |
    #                                  +-- Número de valores sorteados

See working on Ideone

  • Wow, man. vlw, I’ve been racking my brain for days with this exercise. I didn’t know about the existence of this function in Random. I’m going to poke around this function here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.