How do I capture two integers with space between them in python3?

0

I want to capture two integers, for example:

  

2 4

I'm not sure how to do this,

var = input()
lista = []
lista = var.split(' ')
n,q = lista
n = int(n)
q = int(q)
    
asked by anonymous 02.04.2018 / 00:14

2 answers

-2

With space, it would have to be something like this ... But if it's a comma, it can be like this:

[a,b]=list(input("Insira dois numeros separados por virgulas: "))
print "Numero 1:", a
print "Numero 2:", b

In terminal:

Insira dois numeros separados por virgulas: 12,28
Numero 1: 12
Numero 2: 28
    
02.04.2018 / 00:41
2

The function input will always return a string , regardless of the contents read, so if it is necessary to get two integers from a string , separated by a comma , the logic will be basically the same as when separated by a blank space: it only changes the separator character.

For example:

entrada = input('Entre com dois números separados por vírgula:')
x, y = (int(numero) for numero in entrada.split(','))
print(x, y)

In this way, you can also enter entries in the form "2, 3", with the space between the comma and the number, because when converted to integer, space will be ignored.

    
03.04.2018 / 02:27