+ 15

User input python

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

18th Jan 2020, 7:09 AM
Mariiam Raiimzhanova
Mariiam Raiimzhanova - avatar
17 Antworten
+ 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.
18th Jan 2020, 8:09 AM
Lothar
Lothar - avatar
+ 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)
18th Jan 2020, 9:11 AM
HonFu
HonFu - avatar
+ 11
there is a way to do multiple inputs using the walrus operator(:=): inputs = [] while inp := input(): inputs.append(inp)
25th Jul 2021, 8:44 AM
Lothar
Lothar - avatar
+ 5
HonFu, i like your code. And it does also work if you add 2 additional blank lines when running it in PlayGround.
18th Jan 2020, 3:42 PM
Lothar
Lothar - avatar
+ 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
18th Jan 2020, 11:41 AM
rodwynnejones
rodwynnejones - avatar
+ 4
HonFu you're right, that type casting wasn't needed. I forgot that input comes as strings XD
25th Jul 2021, 12:12 PM
Diego Rodrigues
Diego Rodrigues - avatar
+ 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())
18th Jan 2020, 6:16 PM
Genee
Genee - avatar
+ 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 ;)
24th Jul 2021, 11:19 PM
Diego Rodrigues
Diego Rodrigues - avatar
+ 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?
25th Jul 2021, 6:46 AM
HonFu
HonFu - avatar
+ 1
sys.argv Command line argument
18th Jan 2020, 7:23 AM
Prathvi
Prathvi - avatar
0
#this is the method you can use inputs = [] while inp := input(): inputs.append(inp)
14th Sep 2021, 2:31 PM
underscorecoder
underscorecoder - avatar
0
while True: try: inputtext = input() print(inputtext) except EOFError: break
28th Oct 2021, 12:12 AM
Bavikatti Shwetha
0
By creating a loop with a condition of if the user enters a specific entry for example: -1, it will break the loop
14th Nov 2021, 9:30 PM
Khalid Alanazi
Khalid Alanazi - avatar
0
I never knew it that way thxs
31st Dec 2021, 2:18 PM
aldnr lcldkkr
aldnr lcldkkr - avatar
0
I=input() print(I)
11th Apr 2022, 5:08 PM
Jena
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
19th Apr 2022, 4:12 PM
Mehran Janghorbani
Mehran Janghorbani - avatar