Read more than one line with input

Asked

Viewed 164 times

-1

I’m trying to read several numbers in this format and save them in a list:

2030706155721618
3230170874838235
4782877637240802
2406118837772520
4886405615781541
1520748465125584
1766850807841275
4886758384780627
1434406268685305
3788744200728256

Code:

l = str(input('Lista: ')
print(l)

I’m pasting all the numbers at once, but only prints the first item:

2030706155721618
  • How are you inputando the data? Type one number, ENTER, type another, ENTER, or you copied the numbers (Ctrl-C) and pasted (Ctrl-v) all at once? I ask because you already had 4 answers (one deleted) and none solved, maybe because it is not clear how the data entry is being made...

  • I copied them all at once

  • however when executing and printing the first item 2030706155721618

  • I intend after printing all these elements to make a list for other purposes

3 answers

3


According to the documentation, input reads a line and returns it as a string ("reads a line from input"). And as you said you’re pasting all the numbers at once (and they’re one in each row), the input stops reading when it encounters a line break and so only reads the first.

So an alternative is to make one loop and go reading the lines and adding them to the list:

print('Digite os números:')
dados = [] # lista contendo os dados digitados
while True:
    linha = input()
    if linha == '': # linha vazia, interrompe o while
        break
    dados.append(linha)

I mean, even if you do copy-Paste of the numbers, having one in each line, they will be read correctly. Each line will be read by input and added to the list.

One detail is that we need to define some stop condition. I made it as an empty line (ie if only type ENTER), then after pasting the numbers, you’ll have to type in a ENTER.

I didn’t put the message on input because otherwise she would be shown over and over again. But as you are pasting all the numbers at once, it makes no sense to see the same message several times, so I only printed once at the beginning, before the loop.

Once you have the list, you can print as you like. For example:

for n in dados:
    print(n)

It is also worth noting that input already returns a string, so do str(input()) is redundant and unnecessary.


Another way to read these numbers is:

dados = list(iter(input, ''))

Who basically does the same thing: iter(input, '') creates an iterator that calls input several times, until the returned value is '' (the empty string). And finally, list creates a list of the values obtained.

  • 2

    Random note: if there’s anything I forget that exists in Python it’s that iter has the parameter sentinel kkkk

  • 1

    @Woss I found out these days :-)

  • thank you very much.

-1

lo :)

First on your question "input only print first item"

input from English to Portuguese = "Input

When you need to enter some number for the program to do some procedure use that function, example :

l = str(input('Lista: ') # o nome Lista entre aspas simples é como um titulo, o str() é um indicativo que só sera um texto.
print(l) # o print ira mostrar o que foi digitado no input

If it’s to present the numbers it will be so:

lista =[
4782877637240802,
2406118837772520,
4886405615781541,
1520748465125584,
1766850807841275,
4886758384780627,
1434406268685305,
3788744200728256]

for verLista in lista:
  print(verLista)

input reference

list reference

If it’s nothing like that, and have this sequence of number and all have this pattern and number amount without the comma and want to list them,:

lista ="4782877637240802" # padrão
print(len(lista))
print(lista[0:len(lista)])

This way even if all grouped numbers do not have the comma can manipulate and recreate a list with all in the sequence by slicing.

  • yes friend however when run l = str(input('List: ') and print the resulting only and displaying the first item ie 2030706155721618

  • my doubt is only because when I execute only and return me the value 2030706155721618 and not all

  • in his doubt, put everything without comma. What you can do is slice the entire unit and separate them according to the pattern, like everyone there noticed that you have 16 position. All this together would give a huge block that can divide everything into 16 characters.

  • good solution more it is possible to insert the numbers via input and then do this procedure?

  • yes, you can slice any type of data, whether it’s a sentence or the sequence of numbers :)

-2

You can try to do that:

lista = [2030706155721618,3230170874838235,4782877637240802,2406118837772520,4886405615781541,1520748465125584,1766850807841275,4886758384780627,1434406268685305,3788744200728256]

for n in lista:
  print(lista[n])

Explanation: for is a looping, but with a limit. Let’s say that the variable "n" created within the "for" command are all items in your list. And since "for" has a repeat limit, the amount of items in your list will be the limit. And n will go through all these items, and shortly after that your print will write the list and the item you will write is n.

  • I need to enter the Data via Input however when doing this only the value 2030706155721618 and returning without the others.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.