r/programminghelp • u/armsofasquid • Mar 07 '19
How to save many input strings as variables
-I'm new to programming, I've searched and couldn't find anything, yata yata yata-
PYTHON
The best way I can think of asking is by describing what I want the output to be:
-------
Enter John's line:
> Hi
Enter Jane's line
> Hello
Enter John's next line
> Sup
Enter Jane's next line
> STOP
---------
And I would like each of those, ad infinitum, to be labled as 'p1_line1' 'p2_line1' 'p1_line2' et cetera, without having to limit the amount of lines that can be written.
Bonus points if you explain the best way to recall them later :)
1
u/TreeBaron Mar 08 '19
So, probably the best way to do this would be with object oriented programming, but I'm going to assume you have not covered that.
I recommend you look into that, since that would be the best way to do this. Otherwise, I would probably set it up as follows:
Make a list of lists.
Set the person's name at the beginning of each list.
Iterate over the list, displaying their names (the first item of the list) and adding the user's input to the end of each list.
If the user enters STOP, you stop doing this.
You can then display their lines with something like this:
iterate over each list...
for i in range(0:len(ListOfLines)):
if(i == 0): #print everyone's name
print(ListOfLines[i],"'s lines:")
else:
#print everyone's lines...with numbers xD
print(i,". ",ListOfLines[i])Disclaimer: I don't code in Python a lot.
0
1
u/marko312 Mar 08 '19
You could create two lists (one for each person) and then append to them while you have more lines.
This won't produce exactly what you want, but is usually standard for storing a large amount of related values. The list is accessable with
a[x]where a is the list and x is the index of the value you want (starting from 0).