-1

Hi I am trying to create a code that will append items to a list in python through the program itself inside of terminal.

Whenever I use this code it works but when I stop running the code and rerun the new item in the list is gone.

This is currently my code. 'Calc' is a list that has multiple strings so I want to add another item to that list whenever they type that into terminal.

elif userInput in command:
print("Okay, Initializing new command.\n\n")
command1 = raw_input("Which command would you like to add?\nKeyword\nLater Versions\n ").lower()
if command1 == 'keyword':
    print('You selected keyword, to exit press enter')
    command2 = raw_input("Which Keyword would you like to edit? ")
    if command2 == 'calc':
        command3 = raw_input("Which Keyword would you like to add? ")
        calc.append(command3)
        print(calc)
Hello Name
  • 37
  • 7
  • 2
    Of course it's gone because your list is in memory. When you stop your program, everything stored in memory (RAM) is gone. If you want to keep result, write it into file. – cuongnv23 Aug 15 '16 at 04:08

1 Answers1

1

When you re-run, the list is re-initiazing and clearing itself.

One way to save the contents of the list for multiple runs of the code would be to write the contents of the list to a text file and save that.

You can read www.pythonforbeginners.com/files/reading-and-writing-files-in-python for a basic rundown on how to do that.

  • Do you by chance know how to append a word to a specific spot in a text file (line and place in that line) thank you – Hello Name Aug 15 '16 at 04:11
  • This may help: http://stackoverflow.com/questions/4719438/editing-specific-line-in-text-file-in-python. –  Aug 15 '16 at 04:13