To solve the problem of "concatenate" the numbers just use mathematics, if you are using the year with 4 digits as the basis, just multiply the year by 10,000, ie, you will add 4 zeros to the right of the year. After that just add to the next number.
2018 * 10000 + 1 # 20180001
2018 * 10000 + 2 # 20180002
2018 * 10000 + 3 # 20180003
# ...
Example of a Generator who does what you need:
def numero_solicitacao(ano = None, numero_inicial = 1):
ano_atual = ano or date.today().year
numero = numero_inicial
while numero <= 9999:
yield ano_atual * 10000 + numero
numero += 1
gen_numero = numero_solicitacao()
print(next(gen_numero)) # 20180001
print(next(gen_numero)) # 20180002
print(next(gen_numero)) # 20180003
gen_numero = numero_solicitacao(2011, 500)
print(next(gen_numero)) # 20110500
print(next(gen_numero)) # 20110501
print(next(gen_numero)) # 20110502
Note that I set the parameters ano
and numero_inicial
so that you can start at the number that is required, that way you can take the last record that you have in the database and use Generator to follow from that number on.
Repl.it with the code working.
Hello Gustavo, want to return a variable of type
2018+ (4 dígitos aleatórios)
?– Tuxpilgrim
this friend however not adding up the values already tried to create the variable picking the year and another the random number but either returned the sum of the two or a tuple.
– Gustavo Cruz