How to do the input () stop after the space instead of the line break in Python3

1

I want to capture several numbers in this form:

2 4 5 3 5 0 4 

As undefined number of numbers and without using an auxiliary list, I want to use only one variable

for _ in range(0,n):
    num = int(input())

I only know how to create list

for _ in range(0,q):
    lista +=[int(input().split(' '))]
    
asked by anonymous 18.06.2018 / 01:52

2 answers

0

Actually the question is confusing but if the list is "infinite", just read an entire line and then separate the elements:

#!/usr/bin/python3

# recebe cada número de uma só vez.
print('Digite a lista de números separada por espaços, <Enter> finaliza')
entrada = input('=> ')

# separa a string por espaços e converte em uma lista de inteiros.
lista = [ int(i) for i in entrada.split(' ') ]

# imprime a lista, apenas para referência.
print(lista)
    
18.06.2018 / 02:14
0

It is an extremely confusing question and for the little that I understand you want to find a value " " the system stops, if not correct the question. Anyway follows the solution to what I said:

import sys

lista = int(input("Digite a lista"))
newLista = lista.split(' ')

for i in newLista:
    if(i == " "): #Identifica o " "
        sys.exit() #O sistema irá parar

If you want to separate each term from the list, you will have to use another .split() and another label. The input data must contain such a marker.

    
18.06.2018 / 02:06