how should I do for the python code I made for lottery did not repeat the numbers on the same when I put 5 pair and 1 odd to give the game result

Asked

Viewed 94 times

-4

from random import randint

n = int(input('Número par: '))
b = int(input('Número impar: '))
quant = int(input('quantos grupo são: '))

def method_name():
    def sorteia(lista):
        cont = 0
        c = 0
        while True:
            num = randint(1, 61)
            if num % 2 == 0 not in lista:
                lista.append(num)
                cont += 1            
            if cont >= n:            
                break
        while tuple:
            num = randint(1,60)
            if num % 2 == 1 not in lista:
                lista.append(num)
                c += 1            
            if c >= b:
                break
        lista.sort()
        jogos.append(lista[:])
        lista.clear()
        
    lista = list()
    jogos = list()
    sorteia(lista)
    for i, l in enumerate(jogos):
        print(f'jogos {i + 1} : {l}')
    return i, jogos, l, lista, sorteia

i, jogos, l, lista, sorteia = method_name()
  • What is the variable for quant?

2 answers

1

Thinking that you have the numbers set from 1 to 60. I believe that creating the set of pairs and the set of odd ones before saves a lot of trouble

import random

pares = range(2, 61, 2)
impares = range(1, 61, 2)

qtd_par = int(input('Número par: '))
qtd_impar = int(input('Número impar: '))

pares_sorteados = random.sample(pares, qtd_par)
impares_sorteados = random.sample(impares, qtd_impar)

sorteados = pares_sorteados + impares_sorteados
sorteados.sort()

print(sorteados)

If you choose 5 pairs and 1 odd the output will be something like the example down below:

[4, 10, 16, 20, 39, 60]

0

From what I understand you want to generate groups of numbers that will be used as guesses in lottery games, where you must specify the amount of values pairs and also odd values.

Well, first of all I noticed that your range is range(1, 61). With this information I deduce that the expected lottery game is Mega Sena. Thus the SMALLER NUMBER COUNT to be used in each bet (group) of this game is 6. In this case, we will have to draw 6 numbers that will form each group.

Another important thing is to know that to carry out draws without repetitions we need to use the method sample bilbioteca Random. Moreover, we must be aware that as the amount of numbers drawn per group will always be 6, the amount of odd numbers will always be the difference between 6 and the amount of even numbers. Therefore, by specifying the quantity of even numbers we will automatically obtain the quantity of odd numbers, that is, we do not need input capture() the amount of odd numbers.

In addition we must treat the amount of pairs and groups. There are several ways to perform such treatment. In this issue I will use a resource that is available from the Python 3.8, who is called Assignmente Expression.

With all these observations we can implement the following code:

def loterias(p, g):
    for j in range(1, g + 1):
        yield sorted(sample(range(2, 61, 2), p) + sample(range(1, 61, 2), 6 - p))


if __name__ == '__main__':
    while (par := int(input('Quantidade de pares entre "1" e "6": '))) < 1 or par > 6:
        print('Valor INVÁLIDO!')

    while (grupo := int(input('Quantidade de grupos (valor maior que 0): '))) < 1:
        print('Valor INVÁLIDO!')

    resposta = loterias(par, grupo)

    for r in resposta:
        for m in r:
            print(f'{m:02}', end=' ')
        print()

Note that when we execute this code we are asked the amount of even values who will participate in each drawn group. This amount must be a value between 1 and 6. While the entered value for this quantity is less than 1 or greater than 6, we will receive the message Valor IINVÁLIDO! and we are again asked the value.

We are then asked for the number of groups that will be formed. This amount must be greater than 0. While the entered value for this quantity is less than 1, we will receive the message Valor IINVÁLIDO! and we are again asked the value.

This information will then be sent as parameters to the function lotteries(). Arriving there will be drawn the quantity of even numbers and, also, the quantity of odd numbers - that would be (6 - quantity of even numbers). Subsequently, these values will be added to a list, being the same ordered increasingly with the help of the function Sorted().

After these operations, with the help of Yield a function will be mounted generator. Then the for block will display the results in ascending order, properly formatted, with two digits - complete with 0 the missing value on the left.

Testing the code

Imagine we want to draw lots 4 groups, where each of them should have 5 even values and 1 odd value. For this we must run the program and when the message is displayed: Quantidade de pares: , we should type:

5

...and when the message is displayed: quantidade de grupos: we should type:

4

The program will then calculate the results and 4 groups, of 6 numbers, with 5 even numbers and 1 odd number.

As the draw is carried out randomly, one of the possible exits can be:

12 24 29 30 52 60 
12 13 18 36 50 56 
05 14 18 24 54 60 
05 06 18 30 32 58 

Note that the result showed us 4 groups, with 6 values, where 5 of them are even and 1 of them is odd, and all organized in ascending order and with two digits.

Browser other questions tagged

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