User input python | Sololearn: Learn to code for FREE!


User input python

How can I create an input where user can add as many inputs as he/she wants?

1/18/2020 7:09:56 AM

Mariiam Raiimzhanova

17 Answers

New Answer


A very common way to give a user the possibility for a various number of input values, is to use split(). You have to decide what separator should be used. So let's say we use a ",". inp = input('Enter a Name, followed by some integers (sep. by comma)').split(',') If user entered: Bob,2,5,8,0,1 you get a result in inp as a list: ['bob', '2', '5', '8', '0', '1'] Now you can process this list by eg using a for loop.


My favourite way (aside from Code Coach) would be something like this: inputs = [] while True: inp = input() if not inp: break inputs.append(inp)


there is a way to do multiple inputs using the walrus operator(:=): inputs = [] while inp := input(): inputs.append(inp)


HonFu, i like your code. And it does also work if you add 2 additional blank lines when running it in PlayGround.


Here's something I made a few weeks around with it to see if it suits your needs;- mystring = "" counter = 1 delimiter = '#' # <-change this to what ever you need. print("Enter line, end with \"{}\"".format(delimiter)) while True: line = input("Line No " + str(counter) + ":- ") if delimiter in line: mystring += line.replace(delimiter, '') break else: mystring += line + " " counter += 1


HonFu you're right, that type casting wasn't needed. I forgot that input comes as strings XD


One way of approaching this problem is asking for the user how many inputs to be taken and then you can take the inputs. inputs = [] no_input = ("Number of inputs: ") For i in range(no_input): inputs.append(input())


HonFu code is good, but I'd change some elements to show the program's intent to the user, as shown in the code below: inp = [] while True: inp.append(str(input("Type your input: "))) yn = str(input("Do you want to exit and list your inputs?(Y/N): ")) if yn.upper() == "Y": break print(inp) I added type casting just to be sure that our inputs will be strings, in case we need to manipulate them later in an improvement. I converted the second input to upper case too, just so we let the user input his Y in both upper and lower case, and it'll make no difference for the program. I hope you all do well ;)


Diego Ferreira Lopes Rodrigues, the type casting is not necessary: Input in Python is *always* string. Also, imagine you have to input several hundred values. Do you really want to make an extra input every single time just so that you can go on?


sys.argv Command line argument


#this is the method you can use inputs = [] while inp := input(): inputs.append(inp)


while True: try: inputtext = input() print(inputtext) except EOFError: break


By creating a loop with a condition of if the user enters a specific entry for example: -1, it will break the loop


I never knew it that way thxs


I=input() print(I)


After getting the first input ask her if he wants to enter another value, and put it in a while loop, as long as the user enters yes, the cycle will be repeated