How to change the value of a variable in the python terminal and save permanently?

Asked

Viewed 214 times

-2

Does anyone know how to change the value of a variable in the terminal and stay saved forever in the script?

ex: I create a variable of type input ps: The variable name is Name;

E depois crio uma variável com o nome recebe;

A variável recebe a variável Nome;

E quando fecho o terminal do python, a variavel recebe estará o oque eu escrevi no terminal.
  • 3

    The question is not very clear, could you [Dit] explain it better, please? If you are talking about the Python REPL, I think that what you want is not possible.

  • 3

    It is not even possible, if you want to persist the value of a variable when the program is no longer running, you need to save this value somewhere (file, database), and read from this place to get the value saved if it exists.

1 answer

0


Since Voce something simple can use a file to save, but when you want to save data in persistent mode I advise to use some DBMS.

#ler nome do arquivo
with open('data.txt','r') as f:
    name = f.read()
#imprime na tela
print('Atual:')
print(name)
new_name = input('Qual o seu nome:')
with open('data.txt','w') as f:
    f.write(new_name)

remember to create the data.txt file in the same code folder and run in python3

Browser other questions tagged

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