How to add unlimited input with a single row of code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to add unlimited input with a single row of code?

now i am making listing code. i want the input system is always open for every input for the list, without write the code for every input invidually. in other word the input is unlimited. the input value is stored in the list. despite according what i've tried, my code only accept input as much the input statement wrote

26th Oct 2019, 2:22 PM
Muhammad Saleh Alatas
Muhammad Saleh Alatas - avatar
7 Answers
0
Why does it have to be in one line?
26th Oct 2019, 2:43 PM
Russ
Russ - avatar
0
In one line... you can’t, but you can create a thread (make sure to set it as a deamon thread in order to avoid problem when the program finish), the thread must call a function wich is a infinite loop (while True:) and inside the loop just append the input to the list, for better better control you can also have a global variable to control if the input is enabled or disabled, you can use it with to nested loops while True: while var: input() for that, var must be. boolean
26th Oct 2019, 3:28 PM
Elias
0
Do you mean something like this:- myint = int(input("Enter number of inputs required:- ")) mylist2 = [input() for x in [a for a in range(myint)]] print(mylist2) # or print(*mylist2)
26th Oct 2019, 5:04 PM
rodwynnejones
rodwynnejones - avatar
0
You can make a rule in your program, where for example each space separated words are treated as separate inputs. list1 = input(": ").split(" ") If input was "Bear ate 20 strawberries.", the list1 would equal ["Bear", "ate", "20", "strawberries"]. String method split can anyways return empty strings, if there are 2 matches in row. "4 spaces".split(" ") would evaluate to ["4", "", "", "", "spaces"] and those empty lists might not be wanted. But you can easily filter those: for i in range(len(list1)-1, -1, -1)): if list1[i] == "": list1.pop(i) (Used range(len(list1)-1, -1, -1)) because the lists size would change and thus it is smart to remove items starting from the end of the list.)
27th Oct 2019, 7:40 AM
Seb TheS
Seb TheS - avatar
0
Good idea Seb TheS . Actually, if you leave the argument empty in the split() method, spaces will be ignored. So "4 spaces".split() would become ["4", "spaces].
27th Oct 2019, 10:05 AM
Russ
Russ - avatar
0
Russ I know that. .split(" ") to tell that you can use any string to be used to split the input.
27th Oct 2019, 10:23 AM
Seb TheS
Seb TheS - avatar
0
Seb TheS ok. I must be misunderstanding your point as it seems much easier to me to use split() and have your list ready for you (and thus do it in one line which the question asked for) , than to use split(" ") and have to remove all the empty strings from the list.
27th Oct 2019, 10:30 AM
Russ
Russ - avatar