Print list within another list

0

I have the following statement:

  

A teacher takes measurements of the heights of his students in his class.   For each of the 10 students, take the measurements twice a year.   We will make a program that allows us to enter 10 height values   (For the first measurement), and 10 values for the second measurement. the   program will show us a list of the growth experienced for   each student.

I have done this:

altura=[[],[],[],[],[],[],[],[],[],[]]

i=0

print(altura[i][i])
while i <= 10:
    primera_altura=float(input("Introduce altura: "))
    altura[i][i]=primera_altura
    i=i+1

Obviously it does not work, what I'm trying to do is create a list that has 10 sublists for each student, and then ask the user for a height and add it to field i in the list, so each time you enter a height the will enter in another sublist. The problem is that it gives me an error message saying that it leaves the index of the list.

How could I do it?

Thank you very much.

    
asked by Marc 03.06.2018 в 17:23
source

2 answers

1

What you need is a for cycle, iterate over each item in the list, ask for the value (height) you want to assign to the list and add it to the list, so:

lista=[[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]

var_filas=10
var_columnas=2

for row in range(var_filas):
    for col in range(var_columnas):
        altura=input("Introduce altura: ")

        lista[row][col]=altura

print(lista)

As you see, all the values in the list start at 0, and what we do is replace them with the number we want.

var_filas=10

Is the number of rows in the list and

var_columnas=2

It is the number of columns in the list, which is the same as saying that there are 10 elements that contain 2 values each.

If you only want to be able to add decimal numbers to the list modify the input, indicating that you can only add floating

altura=float(input("Introduce altura: "))

I hope I have been of help, greetings, luck!

    
answered by 03.06.2018 / 18:06
source
1

You have two problems:

  • while i <= 10 : iterates from 0 to 10, which causes an indexing error since the indexes in Python are base 0, so in a list of 10 elements the Indexes range from 0 to 9, 10 is not a valid index.

  • altura[i][i] : Your sublists are empty, so you can not index them. You can only index if the list contains an element in that position previously. For example, for lista = [[]] , lista[0] is valid, but lista[0][0] not given that the nested list has no elements. For lista = [[1]] , lista[0][0] is valid and returns 1 , but lista[0][1] is not a valid index.

    On the other hand, it should be in any case altura[i][0] for the first measure and altura[i][1] for the second measure.

Or you initialize your nested lists with initial values, eg:

altura = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]

or use list.append to dynamically add items to the list.

Instead of a cycle while is more efficient and "pythonic" use a for :

altura=[[], [], [], [], [], [], [], [], [], []]

for lista in altura:
    primera_altura = float(input("Introduce altura: "))
    lista.append(primera_altura)
  

Always use a for in to iterate over an iterable instead of indexed unless you change the length of the iterable while iterating over it . In this case the for iterates over altura (which is not modified, only the lists that contain it are).

     

A for is generally preferable to a while whenever possible. Better to use for i in range(10): than% while as you use. Reserve the while for when the number of iterations is not defined in advance.

    
answered by 03.06.2018 в 18:08