Amounts of games in a betting code and their random numbers in ascending order in Python

Asked

Viewed 141 times

0

The lottery draw code produces 6 random dozens and would like to know how to keep these numbers in ascending order, besides define the amount of games the user wants to be generated.

# Como que o código fosse
Quantos jogos deseja realizar: 3
--- Números sorteados ---
2 | 6 | 25 | 31 | 45 | 58
8 | 16 | 28 | 40 | 41 | 42
4 | 10 | 19 | 34 | 47 | 50
# Meu código que produz apenas um jogo sem nenhuma ordem das dezenas
import random
def loteria():
    jogo = list(range(1, 60))
    random.shuffle(jogo)
    print ("--- Número da sorte ---")
    print(" | ".join([str(i) for i in jogo[:6]]))
loteria()

3 answers

5

A practical alternative to this is to use functions that the library random already offers you:

from random import sample

def loteria() -> list:
    return sorted(sample(range(1, 61), 6))

print(loteria())  # [6, 13, 15, 25, 32, 60]

It is worth noting that the correct is range(1, 61), for range(1, 60) generates the sequence [1, 59], not including the number 60.

To allow the amount of games to be set, simply receive a parameter in the function:

from random import sample
from typing import Iterator, List

def loteria(n: int) -> Iterator[List]:
    for _ in range(n):
        yield sorted(sample(range(1, 61), 6))

But that doesn’t guarantee you’ll be drawn n distinct sequences. A simple way to do this would be to store the generated sequences and verify that it is not duplicated:

from random import sample
from typing import Iterator, List

def loteria(n: int) -> Iterator[List]:
    sequences = []
    while len(sequences) < n:
        sequence = sorted(sample(range(1, 61), 6))
        if sequence not in sequences:
            yield sequence
            sequences.append(sequence)

You can also work with sets, set, which by definition do not allow duplicated values, but for this to happen, you will need to pass the return of sorted, which is a list, for a guy hashable, as a tuple, for example.

  • Bro 4 lines of code, crazy thing kkk, but why def loteria() -> list: why this arrow, I thought it would give syntax error.

  • But I missed the amount of games that the user wants to play, but it’s okay I find a way to enter it into the code

  • @Alexf. Arrow sets the return type annotation. While doing loteria() -> list am stating that the function loteria will return a list. You can read about this here

2

You can use the function sorted to get the ordered version of a list, which by default is increasing:

def loteria():
    jogo = list(range(1, 60))
    random.shuffle(jogo)
    print("--- Número da sorte ---")
    print(" | ".join([str(i) for i in sorted(jogo[:6])]))

As for playing several times, it is a matter of receiving a input and turn it into whole; then just repeat the call to the function loteria the number of times desired:

# Perguntar quantidade de jogos
n_jogos = input('Quantos jogos?')

# Transformar string de resposta em inteiro
n_jogos = int(n_jogos)

# Jogar jogo tantas vezes quanto o número de jogos
for _ in range(n_jogos):
    loteria()
  • Exactly where I insert these lines into the code?

  • 2

    @Alexf. Just read the answer code and try to understand what has been done and what is the difference to your code that will understand.

1


You can modify your draw function so that it is able to draw a specific amount of games, returned a array two-dimensional, look at that:

import random

def loteria( qtd ):
    jogos = []
    dezenas = list( range(1, 61) )
    for i in range( 0, qtd ):
        random.shuffle(dezenas)
        jogos.append( sorted(dezenas[:6]) )
    return jogos

print("------ Numeros sorteados ------")
for jogo in loteria( 10 ):  # Gerando Dez Jogos
    print("| {:02d} | {:02d} | {:02d} | {:02d} | {:02d} | {:02d} |".format(*jogo))

Possible exit:

------ Numeros sorteados ------
| 05 | 09 | 21 | 30 | 39 | 53 |
| 03 | 18 | 38 | 47 | 49 | 53 |
| 05 | 15 | 22 | 34 | 38 | 41 |
| 15 | 40 | 42 | 45 | 49 | 51 |
| 04 | 27 | 29 | 30 | 33 | 41 |
| 02 | 16 | 25 | 37 | 45 | 53 |
| 05 | 20 | 26 | 46 | 52 | 55 |
| 21 | 42 | 45 | 55 | 58 | 59 |
| 02 | 07 | 27 | 33 | 48 | 55 |
| 15 | 25 | 35 | 40 | 44 | 53 |

See worked on Ideone

  • the variable qtd I still don’t understand why she’s in the code and why .format(*jogo) I’ve never seen this kind of syntax in `.format

  • qtd is the amount of games that the function loteria() will generate. The asterisk before the array identifier can be better understood in this question

Browser other questions tagged

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