1

I wanna store in a list all user inputs collected over time. I did something like this:

reactions = []
reaction = raw_input("I wanna know your reaction, yo: ")
reactions.append(reaction)

but even after refreshing, my code looks exactly the same, an empty list.

7
  • 3
    if you print reactions it will have one input, but when you restart the program reactions is re-initialized to an empty list Commented Jan 3, 2017 at 18:21
  • Do you want to run the script multiple times, saving all the reactions? In that case you'll have to write the reactions to a file so it isn't cleaned up when your program ends Commented Jan 3, 2017 at 18:21
  • @depperm ohhh so it doesn't have the power to change my code? Commented Jan 3, 2017 at 18:21
  • user input that goes into variables is not 'your code'. your code is what you typed. Commented Jan 3, 2017 at 18:22
  • it changes reactions while the program is running, but once it stops it goes away Commented Jan 3, 2017 at 18:22

3 Answers 3

4

Let's just save them to a file

reaction = raw_input('Please React: ')
with open('reactions.txt', 'a') as f: #a is append mode
    f.write(reaction + '\n')
Sign up to request clarification or add additional context in comments.

Comments

0

When I run your code and print reactions I get whatever I entered. If you want to collect a lot of reactions, you could make a loop, which ends once the user enters "q":

reactions = []
reaction = raw_input("I wanna know your reaction, yo: ")
while reaction != "q":
    reactions.append(reaction)
    reaction = raw_input("I wanna know your reaction, yo: ")
print(reactions)

Comments

0

I don't know what you mean by "refreshing". But your code creates a new empty list at the beginning, so if you run it again, it will always append to a new empty list. Try to see your code as a list of instructions that execute one after another in sequence.

maybe add at the end a way to show the contents of the list print(reactions) and a loop to repeat the last two lines multiple times:

reactions = []
while True:
    reaction = raw_input("I wanna know your reaction, yo: ")
    reactions.append(reaction)
    print reactions

2 Comments

Thank you for the suggestion, but I ran your code and I wanted something that only asks for a reaction once then ends; and everytime you rerun the program it would print all the reactions from previous players including yours.
Then you must store the data somewhere else. Lists don't persist between runs, you must use a database, or a file, or something permanent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.