14

I know how to take a single input from user in python 2.5:

raw_input("enter 1st number")

This opens up one input screen and takes in the first number. If I want to take a second input I need to repeat the same command and that opens up in another dialogue box. How can I take two or more inputs together in the same dialogue box that opens such that:

Enter 1st number:................
enter second number:.............
4
  • 3
    raw_input opens a dialogue box? How (e.g. in what enviroment) are you running your programs? Commented Sep 11, 2011 at 12:19
  • What i meant by box was the simple small window that opens up to take raw_input Commented Sep 11, 2011 at 12:37
  • 2
    Yes, I figured that. I'm still surprised because raw_input should just write the prompt to sys.stdout and read the input from sys.stdin, and those are usually a terminal, another program's output or a file. If there's GUI happening when you do it, that'd be very unusual enviroment. Commented Sep 11, 2011 at 12:47
  • Note that in Python 3, raw_input has been renamed input. Commented Jun 9, 2022 at 0:42

19 Answers 19

17

This might prove useful:

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

You can then use 'a' and 'b' separately.

Sign up to request clarification or add additional context in comments.

Comments

16

How about something like this?

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 would probably want some error handling too)

1 Comment

This is a really nice way to take input from the user.
4

Or if you are collecting many numbers, use a loop

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

print num

Comments

2

My first impression was that you were wanting a looping command-prompt with looping user-input inside of that looping command-prompt. (Nested user-input.) Maybe it's not what you wanted, but I already wrote this answer before I realized that. So, I'm going to post it in case other people (or even you) find it useful.

You just need nested loops with an input statement at each loop's level.

For instance,

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

Comments

2

You can read multiple inputs in Python 3.x by using below code which splits input string and converts into the integer and values are printed

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)

Comments

2

The best way to practice by using a single liner,
Syntax:

list(map(inputType, input("Enter").split(",")))

Taking multiple integer inputs:

   list(map(int, input('Enter: ').split(',')))

enter image description here

Taking multiple Float inputs:

list(map(float, input('Enter: ').split(',')))

enter image description here

Taking multiple String inputs:

list(map(str, input('Enter: ').split(',')))

enter image description here

1 Comment

Please don't post pictures of text. Instead, copy the text itself, edit it into your post, and use the formatting tools like code formatting.
2
  1. a, b, c = input().split() # for space-separated inputs
  2. a, b, c = input().split(",") # for comma-separated inputs

3 Comments

Provide the explanation of your code and how it solves the Problem?
whenever you want to take multiple inputs in a single line with the inputs seperated with spaces then use the first case .And when the inputes are seperated by comma then use the seconfd case . You implement this to shell or any ide and see the working ..@MittalPatel
This will fail horribly if the user doesn't input exactly the required number of fields.
1

You could use the below to take multiple inputs separated by a keyword

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

Comments

1
List_of_input=list(map(int,input (). split ()))
print(List_of_input)

It's for Python3.

Comments

0

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

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

Then, you can operate on the variables first and second. For example, you can convert the strings stored in them to integers and multiply them:

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

4 Comments

Thats right but isnt this going to open up two boxes...how can i enter both numbers in one box? like embedding both raw_input commands into one dialogue box at the same time
@user899714 In the default cpython environment, raw_input has no graphical user interface. You seem to be using a graphical/educational Python environment. Can you tell us the name of that environment?
Actually i haven't used one yet. i was wondering if i could enhance the look of how i take input from user without getting into the hassle of using a graphical environment just by modifying/changing raw_input command...But i guess it isn't possible.
PythonWin opens up a dialog box when using raw_input. At least pywin32 build 212 does.
0

In Python 2, you can input multiple values comma separately (as jcfollower mention in his solution). But if you want to do it explicitly, you can proceed in following way. I am taking multiple inputs from users using a for loop and keeping them in items list by splitting with ','.

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

print items

2 Comments

probably you would like to add a textual explanation to your solution
Thank you. I have added an explanation.
0

You can try this.

import sys

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

For details, please review,

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

Comments

0

Split function will split the input data according to whitespace.

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

name will get first column, id will contain second column and marks will be a list which will contain data from third column to last column.

Comments

0

A common arrangement is to read one string at a time until the user inputs an empty string.

strings = []
# endless loop, exit condition within
while True:
    inputstr = input('Enter another string, or nothing to quit: ')
    if inputstr:
        strings.append(inputstr)
    else:
        break

This is Python 3 code; for Python 2, you would use raw_input instead of input.

Another common arrangement is to read strings from a file, one per line. This is more convenient for the user because they can go back and fix typos in the file and rerun the script, which they can't for a tool which requires interactive input (unless you spend a lot more time on basically building an editor into the script!)

with open(filename) as lines:
    strings = [line.rstrip('\n') for line in lines]

Comments

0
n = int(input())
for i in range(n):
    i = int(input())

If you dont want to use lists, check out this code

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

There are 2 methods which can be used:

  1. This method is using list comprehension as shown below:

     x, y = [int(x) for x in input("Enter two numbers: ").split()] # This program takes inputs, converts them into integer and splits them and you need to provide 2 inputs using space as space is default separator for split.
    
     x, y = [int(x) for x in input("Enter two numbers: ").split(",")] # This one is used when you want to input number using comma.
    
  2. Another method is used if you want to get inputs as a list as shown below:

     x, y = list(map(int, input("Enter the numbers: ").split())) # The inputs are converted/mapped into integers using map function and type-casted into a list
    

Comments

-1

Try this:

print ("Enter the Five Numbers with Comma")

k=[x for x in input("Enter Number:").split(',')]

for l in k:
    print (l)

Comments

-1

How about making the input a list. Then you may use standard list operations.

a=list(input("Enter the numbers"))

1 Comment

@UmeshYadav Your proposed edit would have turned this into a duplicate of other answers here. This should probably simply be downvoted, and eventually deleted.
-1
# the more input you want to add variable accordingly
x,y,z=input("enter the numbers: ").split( ) 
#for printing 
print("value of x: ",x)
print("value of y: ",y)
print("value of z: ",z)

#for multiple inputs    
#using list, map
#split seperates values by ( )single space in this case

x=list(map(int,input("enter the numbers: ").split( )))

#we will get list of our desired elements 

print("print list: ",x)

hope you got your answer :)

1 Comment

This is just a combination of several existing answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.