Taking multiple user inputs in python

I know how to make one login from a user in python 2.5 :

 raw_input("enter 1st number") 

This opens up one input screen and takes the first number. If I want to make a second entry, I need to repeat the same command and open it in another dialog box. How can I take two or more inputs together in the same dialog box that opens so that:

 Enter 1st number:................ enter second number:............. 
+10
python
source share
13 answers

How about something like that?

 user_input = raw_input("Enter three numbers separated by commas: ") input_list = user_input.split(',') numbers = [float(x.strip()) for x in input_list] 

(You might need some error handling too)

+14
source share

This may be useful:

 a,b=map(int,raw_input().split()) 

Then you can use "a" and "b" separately.

+14
source share

Or, if you collect a lot of numbers, use a loop

 num = [] for i in xrange(1, 10): num.append(raw_input('Enter the %s number: ')) print num 
+3
source share

My first impression was that you had to program the command line with cyclic user input inside that command line. (Nested user input.) This may not be what you wanted, but I already wrote this answer before I realized this. So, I am going to publish it if other people (or even you) find it useful.

You just need nested loops with input instructions at each level of the loop.

For example,

 data="" while 1: data=raw_input("Command: ") if data in ("test", "experiment", "try"): data2="" while data2=="": data2=raw_input("Which test? ") if data2=="chemical": print("You chose a chemical test.") else: print("We don't have any " + data2 + " tests.") elif data=="quit": break else: pass 
+1
source share

You can use below to use multiple inputs separated by keyword

 a,b,c=raw_input("Please enter the age of 3 people in one line using commas\n").split(',') 
+1
source share

You can read multiple inputs in Python 3.x using the code below, which splits the input string and converts it to an integer, and the values โ€‹โ€‹print

 user_input = input("Enter Numbers\n").split(',') #strip is used to remove the white space. Not mandatory all_numbers = [int(x.strip()) for x in user_input] for i in all_numbers: print(i) 
+1
source share

Python and all other imperative programming languages execute one command after another. Therefore, you can simply write:

 first = raw_input('Enter 1st number: ') second = raw_input('Enter second number: ') 

Then you can work with the variables first and second . For example, you can convert the strings stored in them to int egers and multiply them:

 product = int(first) * int(second) print('The product of the two is ' + str(product)) 
0
source share

In Python 2, you can enter multiple comma values โ€‹โ€‹separately (as jcfollower mentions in his solution). But if you want to do this explicitly, you can proceed as follows. I take several inputs from users using the for loop and storing them in a list of elements, breaking them into ','.

 items= [x for x in raw_input("Enter your numbers comma separated: ").split(',')] print items 
0
source share

You can try this.

 import sys for line in sys.stdin: j= int(line[0]) e= float(line[1]) t= str(line[2]) 

See the review for more details.

https://en.wikibooks.org/wiki/Python_Programming/Input_and_Output#Standard_File_Objects

0
source share
  1. a, b, c = input (). split () # for spatial shared inputs
  2. a, b, c = input (). split (",") # for comma-separated inputs
0
source share

The split function separates the input according to a space.

 data = input().split() name=data[0] id=data[1] marks = list(map(datatype, data[2:])) 

the name will receive the first column, the identifier will contain the second column and the labels will be a list that will contain data from the third column to the last column.

0
source share

Try the following:

 print ("Enter the Five Numbers with Comma") k=[x for x in input("Enter Number:").split(',')] for l in k: print (l) 
-one
source share

How to make a list entry. Then you can use standard list operations.

 a=list(input("Enter the numbers")) 
-one
source share

All Articles