bubbiecodes
bubbiecodes

Reputation: 13

Saving inputs into a list?

I wanted to make a program (or whatever the correct terminology is) where it lets you enter a small greeting that gets stored in a list.

What I can't figure out is how I can make the program save that list either in a separate file, or within the program, so that when I open it and run it again I have access to that same list.

This is my function where the user would input their greeting:

def greetings():
     print('Would you like to submit a new greeting?')
     yesno()  # Ignore this
     greeting = input()
     storage = []
     if len(greeting) < 3:
          print("That's too short.")
     elif len(greeting) > 15:
          print("That's too long.")
     elif greeting == 'debug':
          print(storage)
     else:
          storage.append(greeting)

I put that function in:

while True:
     user = input()
     user_length = len(user)
     if int(user_length) < 2 or int(user_length) > 15 and tries < 3:
          tries = tries + 1
          print("Please enter a valid name.")
     else:
          greetings()

Upvotes: 1

Views: 139

Answers (2)

Krishna Vaddepalli
Krishna Vaddepalli

Reputation: 81

import traceback, ast, random
def greetings():
    print('Would you like to submit a new greeting? ')
    # yesno()  # Ignore this
    greeting = input()
    if len(greeting) < 3:
        print("That's too short.")
    elif len(greeting) > 15:
        print("That's too long.")
    elif greeting == 'debug':
        print(storage)
    else:
        storage = []
        try:
            previous = open('data.txt', 'r')
            read = previous.read()
            storage = ast.literal_eval(read) 
            previous.close()
        except:
            print(traceback.format_exc())
            storage = []
            print("File not found")
        storage.append(greeting)
        write = open('data.txt', 'w')
        write.write(str(storage))
        write.close()
        print("Random greeting - {}".format(random.choice(storage)))
        

while True:
    user = input('Enter your name : ')
    user_length = len(user)
    if int(user_length) < 2 or int(user_length) > 15 and tries < 3:
        tries = tries + 1
        print("Please enter a valid name.")
    else:
        greetings()

This reads the file every time there is a new entry and adds new greeting to the end of the list and writes to file. After writing to file, it prints a random greeting from the list

enter image description here

Upvotes: 0

Aryan
Aryan

Reputation: 1113

Answer To Your Question

To Store A Input Permanently You Need You Either Store It In A Database or a file in this case i'am storing this in a .txt file


while True:
  text = input("Please Enter Some Random Text Here:\n")
  # Make Sure That The File `inputs.txt` exists!
  with open('inputs.txt', encoding='utf-8', mode='a') as file: # Mode'a' = append
    file.write('\n'+text)
    file.close()  # To save some memory
    

Update

This Is Just The 'skeleton' for your question 'What I can't figure out is how I can make the program save that list either in a separate file, or within the program, so that when I open it and run it again I have access to that same list.'

Upvotes: 2

Related Questions