How to return the result of a function to a python file

Asked

Viewed 45 times

-2

Could you help me? wanted to return the result of this function in a file for after that generate it send it to another application. I have tried several ways, but even opens the file, but when I look inside it is blank.

    def inventario():
       print('\n*** Ajuste de Inventário ***\n')
       codprod = input('Código do Produto?: ')
       qt = input('Quantidade Contada: ')
       motivo = input('Motivo do Ajuste: ')
       user = input('Solicitante: ')
       return (codprod, qt, motivo, user)
       resultado = open('resultado.txt', 'r+')
       arq = (codprod, qt, motivo, user)
       for x in arq:
             resultado.write(x)
             resultado.write("\n")
             resultado.close()

1 answer

0


The code needs several adjustments, let’s see:
The function inventario() ends in the return (codprod, qt, motivo, user), then the lines of code after the return need to be at the beginning of the line, because the way it was devised, everything would belong to function.

def inventario():
    print('\n*** Ajuste de Inventário ***\n')
    codprod = input('Código do Produto?: ')
    qt = input('Quantidade Contada: ')
    motivo = input('Motivo do Ajuste: ')
    user = input('Solicitante: ')
    return (codprod, qt, motivo, user)

The application is as follows, note that it is starting at the beginning of the line:

resultado = open('resultado.txt', 'r+')
arq = inventario()
for x in arq:
    resultado.write(x)
    resultado.write("\n")

resultado.close() need to start at the beginning of the line also to only close the file after the loop for.

resultado.close()

The whole code goes like this:

def inventario():
    print('\n*** Ajuste de Inventário ***\n')
    codprod = input('Código do Produto?: ')
    qt = input('Quantidade Contada: ')
    motivo = input('Motivo do Ajuste: ')
    user = input('Solicitante: ')
    return (codprod, qt, motivo, user)

resultado = open('resultado.txt', 'r+')
arq = inventario()
for x in arq:
    resultado.write(x)
    resultado.write("\n")

resultado.close()
  • Thank you so much! I am new to the language and I am learning, but it helped me a lot!

  • Tranquil @kurtz_lima, how nice that I helped. Please, since you are new here, I ask you to check How and why to accept an answer.

Browser other questions tagged

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