How to generate repeated guesses for the mega sena game?

Asked

Viewed 140 times

-1

Good evening! I am very beginner in Python and I am trying to implement a function that generates numbers to be used in mega sena games. However, when executing the said function it returns me the following:

[54, 38, 32, 54, 38, 28]

Note that the return of the function is formed by a list containing some repeated values.

I would like to avoid repeated numbers. How can I proceed?

Code

from random import randint
 
 
def gerar_seis_numeros():
    numeros = []
    for i in range(6):
        numeros.append(randint(0, 60))
    return numeros
 
 
def completar():
    return randint(0, 60)
 
n = gerar_seis_numeros()
for i in range(6):
 
    if n[i] != n[i]:
        print(completar())
  • 3

    Luiz Machado, first of all welcome to the platform. Second, if you want to generate draws of numbers to be used as guesses of Mega Sena games, use the method sample library random. Read also How-to No Questions Manual. Hug and always come back.

2 answers

4

From what I understand you want to implement an algorithm that is able to generate game guess from Mega Sena.

Well, as I said in the comment above, we should use the method sample library random.

A possible code would be:

from random import sample


def mega_sena(q):
    for i in range(1, q + 1):
        yield sorted(sample(range(1, 61), 6))


quant = int(input('Quantidade de palpites: '))

for j in mega_sena(quant):
    for k in j:
        print(f'{k:02}', end=' ')
    print()

Note that when we execute the code, we receive the following message: Quantidade de palpites: . At this point we must enter the amount of guesses for the game of Mega Sena. Then, this amount is passed to the function mega_sena(q). Getting there the block for will traverse the range formed by the amount of guesses and, for each interaction i, with the help of the function Yield, a generator formed by the 6 drawn values and unrepeated, within the range(1, 61).

OBS: how we want to draw the values between [1, 60] we should use the range(1, 61).

Later the results of each guess will be displayed.

  • Could explain what exactly this print(f'{k:02}', end=' ') ago?

  • thank you both for helping me.

  • 2

    @Yoyo, the print(f'{k:02}', end=' '), is used to print each number with two digits on the same line. Numbers that do not have two digits (1, 2, 3, 4, 5, 6, 7, 8, 9), are completed with a zero left, leaving (01, 02, 03, 04, 05, 06, 07, 08 09).

2

Hello you can do as follows

from random import randint

def gerar_seis_numeros():
    numeros = []

    while len(numeros) < 6:
        # Executando o random
        x = randint(0, 60)
        # verificando se o numero existe na lista, caso exista não é adicionado
        if x not in numeros: numeros.append(x)

    return numeros

x = gerar_seis_numeros()
print(x)

  • 2

    If you want the function to run enough to add 6 different numbers to the list, there is no need to use a range up to a thousand and add an if. What if the randint() generate equal numbers over a thousand times in a row? the function will stop but the list will not be complete. In this situation the ideal is to use a while len(numeros) < 6:

  • 1

    @Yoyo corrected!

  • Thanks, well cool I’m pretty beginner in this

Browser other questions tagged

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