purchase script, in txt file the name of the objects of the purchases, are not below each other

Asked

Viewed 97 times

0

print('Programa de controle: Lista de compras 1.0')

numero_compras = input('Numero de itens: ')
lista_compras = []

i = 1
while i <= int(numero_compras):
    nome_compras = input('Preencha com o nome da compra #' + str(i) + ': ')
    lista_compras.append(nome_compras)
    i += 1
print('\n')
print('Foi adicionado',numero_compras,"Itens para sua compra.")
# noinspection PyInterpreter
print('\nLista de Compras: ')
for compras in lista_compras:
    print(compras)


arquivo = open('lista01.txt', 'w')
arquivo.write('Sua lista: \n')
arquivo.writelines(lista_compras)
with open('lista01.txt') as arquivo:
    for linha in arquivo:
        print(linha.rstrip())
  • Your strings do not have the line break at the end.

  • this print(line.rstrip())?

  • or all other strings ?

1 answer

0


I made simple changes, no append I added the exhaust character for line break and on print within the repeating structure replaces the variable by shopping name.

Follow the changed code:

print('Programa de controle: Lista de compras 1.0')

numero_compras = input('Numero de itens: ')
lista_compras = []

i = 1
while i <= int(numero_compras):
    nome_compras = input('Preencha com o nome da compra #' + str(i) + ': ')
    lista_compras.append(nome_compras+'\n')
    i += 1
print('\n')
print('Foi adicionado',numero_compras,"Itens para sua compra.")
# noinspection PyInterpreter
print('\nLista de Compras: ')
for compras in lista_compras:
    print(nome_compras)


arquivo = open('lista01.txt', 'w')
arquivo.write('Sua lista: \n')
arquivo.writelines(lista_compras)
with open('lista01.txt') as arquivo:
    for linha in arquivo:
        print(linha.rstrip())

See working on repl.it

  • brother, thank you very much, and here I am cracking my head...

  • I am new, new in part, I stopped programming a few years ago, I studied mechatronics, quit to work and help at home, now I’m back to studying programming.

  • 1

    @Michaeloliver just a suggestion, avoid comments like "thanks", "thank you", the best thanks is you mark the answer as useful by clicking the arrow, and/or if you solved your problem fully, mark the answer as accepted, in the visa :D

Browser other questions tagged

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