Multiple inputs to list. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Multiple inputs to list.

Hello, I wanted to solve one challange by putting multiple inputs given by Sololearn straight to list, but I couldn't figure it out. Of course, if you know number of inputs you could do it manually: i1=input() i2=input() ... in=input () And then put it manually to list list=[i1,i2,....,in] But it won't work if number of inputs is unknown, or there are so many inputs that it's pointless. I thought that I could do something like: list=input().split("\n") But it's not one input in multiple lines, it's multiple inputs. I thought maybe list=[] While input()==True: list.append(input()) But of course it doesn't work. (If someone could explain why it would be great as well) It must be some simple way but I can't figure it out.

17th Oct 2021, 9:58 AM
Piotr Ś
Piotr Ś - avatar
27 Answers
+ 5
try this one hope it will work for you def user(n): print("How many inputs ? ") n=int(input("Enter number of input : ")) alist=[] for x in range(n): alist.append(input()) print(alist)
17th Oct 2021, 10:12 AM
Jasy Fabiano
Jasy Fabiano - avatar
+ 5
Piotr Ś Could you tell me which challenge, and the input details so I can assess further
17th Oct 2021, 10:22 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 4
Piotr Ś your input have to be separated by something. So if separated by new line list=input().split("\n") If separated by commas list=input().split(",") Or by semicolon list=input().split(";") Or simple by blank list=input().split(" ")
17th Oct 2021, 10:22 AM
Coding Cat
Coding Cat - avatar
+ 3
Are you supposed to read a line as list in that challenge? just asking cause in challenges, we're supposed to do *exactly* what was instructed. If the output is checked immediately after the input was provided, but your code wants to read multiple values; what happens then?
17th Oct 2021, 10:25 AM
Ipang
+ 3
Rik Wittkopp It's exactly "Snap, Crackle and Pop" In this challange it's exactly 6 inputs, so nornally you have to define 6 inputs and then put them on list. Coding Cat Yes i know, that's why I thought input was separated by \n, but it's not. Maybe separation of different inputs it's the problem, it's something else than \n. Ipang No no it's just 6 inputs, which I must determine what output each of them gives("Snap", "Crackle" or "Pop") I did this challange manually, but I was wondering is there more parametric approach. Jasy Fabiano Thank you, that's it! It works in Sololearn because you always know how many inputs there will be. In this case you just have to put number of inputs in "n" because you cannot ask Sololearn app about it, they just tell you how many inputs in the description of task 😄 Still, maybe there is something more general, when you don't know number of inputs? Like when someone just put their input one after another? and he can make 3 inputs or 89 inputs?
17th Oct 2021, 10:54 AM
Piotr Ś
Piotr Ś - avatar
+ 2
Piotr Ś It could be possible to get continuous user input & append to list by using a while loop. But you would need to define an exit condition
17th Oct 2021, 10:57 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Piotr Ś did you read my answer up to the end? You CAN split by what you want. You only have to now the used separator.
17th Oct 2021, 11:02 AM
Coding Cat
Coding Cat - avatar
+ 2
If and when you get to this chapter you'll get the hang of it ... https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2440/
17th Oct 2021, 11:02 AM
Ipang
+ 2
Piotr Ś you know how many inputs you have to handle. Read description: Input Format: You receive 6 integers and each integer represents the number of Rice Krispies in your bowls. And then take input like this: bowl = [] for i in range(6): bowl.append(int(input()))
17th Oct 2021, 11:12 AM
Coding Cat
Coding Cat - avatar
+ 2
Piotr Ś Try this using Walrus Operator.. list_ = list() while something := input("Something pls.. "): list_.append(something) print(list_) # Loop will terminate if there's no input in new line.
17th Oct 2021, 12:51 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 2
Piotr Ś 'Something pls..' is just an instruction for user (you can omit it). Try this in console it will take, user input one by one every new line. If you have to terminate this Loop, all you have to do is enter without any input. If there's no loop while Loop will consider it as false and lead to terminate the loop. Run here and try to input your data.. https://onlinegdb.com/OMNIatIOtz Walrus Operator? https://www.sololearn.com/learning/2429/
17th Oct 2021, 3:32 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 2
Rik Wittkopp Yes, I'm fully with you. I wanted only point out this "issue".
19th Oct 2021, 7:54 AM
Coding Cat
Coding Cat - avatar
+ 1
Coding Cat Yes that's it, Jasy Fabiano make this point, that you have to make range with number of inputs. And still, as you can read, I did that challange, now I know that using range(number of inputs) is the answer, but what if you don't know how many inputs? Or you always must know number of inputs to run code in the first place?
17th Oct 2021, 11:17 AM
Piotr Ś
Piotr Ś - avatar
+ 1
Piotr Ś in case you don't know the number of inputs, you have to define a end condition and get the inputs in a while-loop. Up to the point if one input is equal the end condition.
17th Oct 2021, 11:22 AM
Coding Cat
Coding Cat - avatar
+ 1
Piotr Ś yes, that's the idea. Here an example that stops if the input is less then zero. bowl = [] while True: value = int(input()) if (value < 0 ): break bowl.append(value) print(bowl)
17th Oct 2021, 11:43 AM
Coding Cat
Coding Cat - avatar
+ 1
Piotr Ś what do you mean, the input examples above, or the test cases for your challenge?
17th Oct 2021, 12:17 PM
Coding Cat
Coding Cat - avatar
+ 1
Piotr Ś So, now we will talk only about this code coach challenge. Therefore you have only to use this input: list = [] for i in range(6): list.append(int(input())) I got all test cases with this. If you want to try this one in playground, you have to input each value in a new line! 14 (tap return key) 60 (tap return key) 23 (tap return key) 72 (tap return key) 15 (tap return key) 52 (submitt) If you don't got the challenge with this kind of input, something other is wrong.
17th Oct 2021, 2:26 PM
Coding Cat
Coding Cat - avatar
+ 1
Ah ok. That's not possible. You have to know the number of inputs, or you have to define some condition that marks the end of data.
17th Oct 2021, 3:09 PM
Coding Cat
Coding Cat - avatar
+ 1
for name in range(1, rangeofnumber): name = input() list.append(name) Do a for loop in range of numbers. Then the element should be taking the input as a variable, you can then append it to a list
17th Oct 2021, 9:37 PM
Anisa Ali
Anisa Ali - avatar
+ 1
Piotr Ś ''' I was looking further into your question and thought to propose this solution, but it still requires a deliberate action by the user to terminate the loop, which won't happen in the challenges. ''' lst = [] txt = input() while txt != "": lst.append(txt) txt = input() print(lst) #input format #RUN # one ENTER # two ENTER # three ENTER # ENTER # SUBMIT
19th Oct 2021, 6:12 AM
Rik Wittkopp
Rik Wittkopp - avatar