Two values ​​from a single input in python?

This is a somewhat simple question, and I hate asking it here, but I cannot find the answer elsewhere: is it possible to get multiple values ​​from the user in the same Python line?

For example, in C, I can do something like this: scanf("%d %d", &var1, &var2). However, I cannot understand what the Python equivalent is. I figured it would just be something like var1, var2 = input("Enter two numbers here: "), but it does not work, and I do not complain, because it will not make much sense if this happens.

Does anyone know a good way to do this elegantly and concisely?

+29
source share
17 answers

Python path to display

printf("Enter two numbers here: ");
scanf("%d %d", &var1, &var2)

will be

var1, var2 = raw_input("Enter two numbers here: ").split()

, split(' '), split() . , split(), , , , .,

Python , %d. , , var1 var2 . int

var1, var2 = [int(var1), int(var2)]

var1, var2 = [int(x) for x in [var1, var2]]

, :

# Python 3
var1, var2 = [int(x) for x in input("Enter two numbers here: ").split()]

# Python 2
var1, var2 = [int(x) for x in raw_input("Enter two numbers here: ").split()]
+48

( ), ( "" ):

raw_answer = raw_input()
answers = raw_answer.split(' ') # list of 'answers'

, :

var1, var2 = raw_input("enter two numbers:").split(' ')

, , "" (, , ).

, var1 var2 , int.

+10

Python 2.*, input , . :

>>> a, b = input('Two numbers please (with a comma in between): ')
Two numbers please (with a comma in between): 23, 45
>>> print a, b
23 45

Python 3.*, input 2.* raw_input, , ( eval 2.*, input)), .split / eval, & c, .

+8

, a, b python, .
,

1
5 3
1 2 3 4 5

1 , 5 , 3 , 5 , , PYTH 2.x.

testCases=int(raw_input())
number, taskValue = map(int, raw_input().split())
array = map(int, raw_input().split())

'int' map() .

+6

. , . , eval (input()), .

input_string = raw_input("Enter 2 numbers here: ")
a, b = split_string_into_numbers(input_string)
do_stuff(a, b)
+3

split(), . , , , . .

, 23 24 25. 3 ,

num1 = 23
num2 = 24
num3 = 25

, Python

num1,num2,num3 = input().split(" ")
+3

:

def gets(*types):
    return tuple([types[i](val) for i, val in enumerate(raw_input().split(' '))])

# usage:
a, b, c = gets(int, float, str)
+2

, , :


,

value = input("Enter 2 numbers (separated by a comma): ")

: n m

n,m = value.split(',')

, ,

n, m = int(n), int(m)


+2

, , - a, b

a,b=input("Enter two numbers").split()

. ( ) ,

value=input("Enter the line")
a,b,c=value.split()

EASY..

+1

, split ","


>>> var1, var2 = input("enter two numbers:").split(',')
>>>enter two numbers:2,3
>>> var1
'2'
>>> var2
'3'


, , :
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('~')

+1

n :

>>> x=input('Enter value of a and b').split(",")
Enter value of a and b
1,2,3,4
>>> x
['1', '2', '3', '4']
+1
a,b=[int(x) for x in input().split()]
print(a,b)

10 8

10 8

0

Python 3 raw_input() input().

input([prompt])

, . , ( ) . EOF , EOFError .

x, y = input('Enter two numbers separating by a space: ').split();
print(int(x) + int(y))

, , ValueError. .

NB. input(), eval(input())

0
source

I tried this in Python 3, it seems to work fine.

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

print(a)

print(b)

Entrance: 3 44

Output:

3

44

0
source

Two inputs are separated by a space:

x,y=input().split()
0
source

This solution is used to convert multiple lines, for example ("22 44 112 2 34") into a list of integers, and you can access the values ​​in the 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 :))))
0
source

You can do it like this

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

OR

a, b = input().split()
print(int(a))

OR

MyList = list(map(int, input().split()))
a = MyList[0]
b = MyList[1]
0
source

Source: https://habr.com/ru/post/1629257/


All Articles