How to make the program write all information?

0
print('Bem-vindo!')
print()

numero_contas = int(input('Deseja registrar quantas contas: '))
x = 0

while x < numero_contas:
            x = x + 1
            conta = str(input('A conta é de qual site:'))                                  
            print()
            usuário = str(input('Digite o usuário:'))
            senha = str(input('Digite a senha: '))
            print()
            arq = open('Contas.txt', 'w')
            arq.write(conta)
            arq.write('\n')
            arq.write('Usuário: {}'.format(usuário))
            arq.write('\n')
            arq.write('Senha: {}'.format(senha))
            arq.write('\n')
            arq.close()
The problem is this: if for example, I type 2 in the variable "account", it asks for the information normally, but when I open the file to verify, it only registers the first account, if I type 3 in variable "account", it only records the second account, so on. How can I solve this problem?

One more basic question, how can I make sure that the program, whenever it is executed, does not take away the previous records? For example: I ran the program, I registered only one account. Then I run the program again, and register two accounts, then when I go to check, the data from the first run is gone, it only left the second run data registered. And I want it to register all the data, without deleting the data from the previous executions, I want it to join with the next executions.

    
asked by anonymous 06.02.2017 / 20:08

1 answer

0
It's because you're using w , use a (an "extra" is that you really do not need to open the same file several times you can leave the handle of it out of while )

print('Bem-vindo!')
print()

numero_contas = int(input('Deseja registrar quantas contas: '))
x = 0

arq = open('Contas.txt', 'a')

while x < numero_contas:
    x = x + 1
    conta = str(input('A conta é de qual site:'))
    print()
    usuário = str(input('Digite o usuário:'))
    senha = str(input('Digite a senha: '))
    print()
    arq.write(conta)
    arq.write('\n')
    arq.write('Usuário: {}'.format(usuário))
    arq.write('\n')
    arq.write('Senha: {}'.format(senha))
    arq.write('\n')

arq.close()

Or you can put open if you need that every time the program starts it re-generates the list use:

print('Bem-vindo!')
print()

numero_contas = int(input('Deseja registrar quantas contas: '))
x = 0

arq = open('Contas.txt', 'w')

while x < numero_contas:
    x = x + 1
    conta = str(input('A conta é de qual site:'))
    print()
    usuário = str(input('Digite o usuário:'))
    senha = str(input('Digite a senha: '))
    print()

    arq.write(conta)
    arq.write('\n')
    arq.write('Usuário: {}'.format(usuário))
    arq.write('\n')
    arq.write('Senha: {}'.format(senha))
    arq.write('\n')

arq.close()
    
06.02.2017 / 20:11