Put the input data into a list

Asked

Viewed 5,286 times

1

I need to store the 10 numbers typed by the user in a list, only using "for".

for c in range(0, 10):
    s = int(input())

Then I would just need to know how I store each number after being typed and not only the last one, as I currently know how to do.

2 answers

2


First Voce defines an array( s = [] ), then uses the append function to enter the entered data.

s = []
for c in range(0,10):
    s.append ( int(input()) )  
  • This is the only use of this function or it has other uses?

  • 1

    the use of it is just to add an element at the end of the list

1

The default start value of range is 0 soon make range(0,10) corresponds to make range(10) which is simpler.

If you just want to read the various values, you can do it in a pythonic way using list comprehensions with:

numeros = [int(input()) for c in range(10)]

Which will create a list of the 10 values the user has entered.

Documentation for the range

Browser other questions tagged

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