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

+7

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

11 Answers

New Answer

+18

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.

+8

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

+6

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

+6

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

+3

Here's something I made a few weeks back..play 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

+3

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())

+3

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

+1

sys.argv Command line argument

+1

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 ;)

+1

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?

0

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