+ 15
User input python
How can I create an input where user can add as many inputs as he/she wants?
16 Answers
+ 27
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.
+ 16
My favourite way (aside from Code Coach) would be something like this:
inputs = []
while True:
inp = input()
if not inp:
break
inputs.append(inp)
+ 11
there is a way to do multiple inputs using the walrus operator(:=):
inputs = []
while inp := input():
inputs.append(inp)
+ 5
HonFu, i like your code. And it does also work if you add 2 additional blank lines when running it in PlayGround.
+ 4
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
+ 4
HonFu you're right, that type casting wasn't needed. I forgot that input comes as strings XD
+ 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 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 ;)
+ 2
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?
+ 1
sys.argv
Command line argument
0
#this is the method you can use
inputs = []
while inp := input():
inputs.append(inp)
0
while True:
try:
inputtext = input()
print(inputtext)
except EOFError:
break
0
By creating a loop with a condition of if the user enters a specific entry for example: -1, it will break the loop
0
I never knew it that way thxs
0
I=input()
print(I)
0
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