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.
What is the variable for
quant
?– Paulo Marques