PYTHON Saving a result in txt

2

I have this code that generates combinations of numbers When I squeeze, he starts the combinations what I want and that it save these combinations in a txt file

import random

c1 = (random.choice([9, 9]))
c2 = (random.choice([1, 1]))
c3 = (random.choice([9, 9, 9]))

def gerar_randomico():
    return random.choice([2, 3, 4, 5, 6, 7, 8, 9])

def gerar_randomicus():
    return random.choice([6, 7, 8, 9])

for i in range(3):
    print ('{}{}{}{}{}{}{}{}{}{}{}'.format(c1, c2, c3, gerar_randomicus(), gerar_randomico(),gerar_randomico(), gerar_randomico(),gerar_randomico(),gerar_randomico(),gerar_randomico(), gerar_randomico()))
    
asked by anonymous 27.12.2018 / 01:19

1 answer

2

It's just your take and save the numbers in a list, giving an append, and then create a txt file and make it write.

What I did, was to create an empty list at the beginning, and in your for, instead of starting, I kept the number in a list, and there I created the file.

import random

c1 = (random.choice([9, 9]))
c2 = (random.choice([1, 1]))
c3 = (random.choice([9, 9, 9]))
lista = []

def gerar_randomico():
    return random.choice([2, 3, 4, 5, 6, 7, 8, 9])

def gerar_randomicus():
    return random.choice([6, 7, 8, 9])

for i in range(3):
    lista1 = ('{}{}{}{}{}{}{}{}{}{}{}'.format(c1, c2, c3, gerar_randomicus(), gerar_randomico(),gerar_randomico(), gerar_randomico(),gerar_randomico(),gerar_randomico(),gerar_randomico(), gerar_randomico()))
    with open('arquivo.txt','a') as arquivo:
        arquivo.write(str(lista1))
        lista_guardar = lista.append(arquivo)
        print(lista_guardar)
    
27.12.2018 / 01:47