This is somewhat of a simple question and I hate to ask it here, but I can\'t seem the find the answer anywhere else: is it possible to get multiple values from the user in
The easiest way that I found for myself was using split function with input Like you have two variable a,b
a,b=input("Enter two numbers").split()
That's it. there is one more method(explicit method) Eg- you want to take input in three values
value=input("Enter the line")
a,b,c=value.split()
EASY..
In Python 3, raw_input() was renamed to input().
input([prompt])
If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.
So you can do this way
x, y = input('Enter two numbers separating by a space: ').split();
print(int(x) + int(y))
If you do not put two numbers using a space you would get a ValueError exception. So that is good to go.
N.B. If you need old behavior of input(), use eval(input())
If you need to take two integers say a,b in python you can use map function.
Suppose input is,
1 5 3 1 2 3 4 5
where 1 represent test case, 5 represent number of values and 3 represents a task value and in next line given 5 values, we can take such input using this method in PYTH 2.x Version.
testCases=int(raw_input())
number, taskValue = map(int, raw_input().split())
array = map(int, raw_input().split())
You can replace 'int' in map() with another datatype needed.
This is a sample code to take two inputs seperated by split command and delimiter as ","
>>> var1, var2 = input("enter two numbers:").split(',')
>>>enter two numbers:2,3
>>> var1
'2'
>>> var2
'3'
Other variations of delimiters that can be used are as below :
var1, var2 = input("enter two numbers:").split(',')
var1, var2 = input("enter two numbers:").split(';')
var1, var2 = input("enter two numbers:").split('/')
var1, var2 = input("enter two numbers:").split(' ')
var1, var2 = input("enter two numbers:").split('~')
This solution is being used for converting multiple string like ("22 44 112 2 34") to an integer list and you can access to the values in a list.
n = input("") # Like : "22 343 455 54445 22332"
if n[:-1] != " ":
n += " "
char = ""
list = []
for i in n :
if i != " ":
char += i
elif i == " ":
list.append(int(char))
char = ""
print(list) # Be happy :))))
The solution I found is the following:
Ask the user to enter two numbers separated by a comma or other character
value = input("Enter 2 numbers (separated by a comma): ")
Then, the string is split: n takes the first value and m the second one
n,m = value.split(',')
Finally, to use them as integers, it is necessary to convert them
n, m = int(n), int(m)