Loading...

How would you save user data in a list after the program closes without a text.file?


I need help saving user input to a list after the program closes without a .text file (If possible)

I have it as let's say Test_Password = [""] I defined the list but every time the program opens I have to set it to a blank string because if I don't it won't be defined.

     python
def Test1():
    ABC_List = [""] #--i'll have to set the list to a blank string
    def Test2() #--Another function to skip past the function that defines the list
        user_input = input("Already have a letter: [Y]/[N]: ").upper()
        if user_input == ("Y"):
           print (" ")
           x = input("What is your letter: ")
           if x in ABC_List:
              print (" ")
              print ("Awesome! \n")
              Test2()
           else:
                print ("You do not have a letter, go and add one! \n")
                Test2() #-- I have no way of saving the data
        elif user_input == ("N"):
             print (" ")
             x = input("Enter a letter! ")
             if x in ABC_List:
                print (" ")
                print ("Sorry, letter is already in list! \n")
                Test2()
             else:
                  x.append(ABC_List)
                  Test()
                  print ("")
                  Test2()
    Test2()
Test1()
- - Source

Answers

answered 1 week ago brunns #1

If you want some data to be remembered after your program finishes running, it needs to be saved somewhere. A text file of some sort would be one option, but there are many others - a database, various cloud storage things, many many options. These will all be more work than a text file, though.

Why is it you need the data save, and why do you object to a text file? It would be easier to offer a helpful suggestion if we knew the answer to those questions.

Updated:

Since this is a homework question, I'll give you some hints rather than doing all the work for you. :-)

As you've told us, you'll be handing in just the script, so there may or may not be a data file when your script starts. You can try reading the file, and deal with the fact that the file might not be there. In Python, we deal with that kind of issue by catching exceptions.

Trying to load a list from a file, but falling back to an empty list if the file doesn't exist might look something like:

try:
    with open('abc_list.txt') as abc_list_file:
        abc_list = [value.strip() for value in abc_list_file]
except IOError:
    abc_list = []

You can add to this list as you want to in your program. When you have a list you want to save, do something like:

with open('abc_list.txt', 'w') as abc_list_file:
    abc_list_file.writelines(abc_list)

answered 1 week ago sleblanc #2

You cannot save state without performing IO (input/output). If you cannot save to a local file, your only option is to IO to another machine (that is to say: send your data over the Internet).

Otherwise, to recover state from a file:

file = open('my_things.txt')

# strip() is needed because each line includes a newline character
# at the end
my_things = set(line.strip() for line in file)

file.close()

Verify if an item is in this set:

if "something" in my_things:
    print("I found it!")
else:
    print("No such thing!")

Add something in your set:

my_things.add('something')

Write items back to your file:

file = open('my_things.txt', 'w')

for item in my_things:
    file.write(item + '\n')

file.close()

Combining file operations:

with open('my_things') as file:
    my_things = set(line for line in file)

with open('my_things.txt', 'w') as file:
    file.write(item + '\n')

comments powered by Disqus