Problem with lists - Python [closed]

0

I can not do:

  
  • The ages and heights of 5 students were noted. Make a Program that determines how many students over 13 have height   less than the average height of these students.
  •   

    Final Code:

    idadeAlunos = [12,13,13,15,16]
    alturaAlunos = [1.70,2.0,1.40,1.55,1.70]
    x = 0
    
    for i in range(len(alturaAlunos)):
        x += alturaAlunos[i]
    x = x/len(alturaAlunos)
    
    contador = 0
    
    for j in range(len(idadeAlunos)):
        if idadeAlunos[j] >= 13 and alturaAlunos[j] < x:
            contador += 1
    print(contador)
    
        
    asked by anonymous 25.05.2017 / 19:22

    2 answers

    3

    Do things step by step. Try to isolate the problems.

    First, the problem says that it will be necessary to use the mean of the heights, so first thing, calculate the mean of the heights and store in a variable.

    After that, you need to iterate over the first list (of ages) and for each iterated element check to see if it is eh > that 13 (if element > 13: ....)

    This is done by using the index of the iterated element to find its corresponding height and verifying that it is less than the mean. To find the corresponding height you can use alturaAlunos[idadeAlunos.index(elemento)] where element will be the element being iterated in the loop.

    Last tip, use a for loop to iterate over the idadeAlunos list.

    Post your code if you have solved the problem, if you can not, post it so I can help solve it.

    Whenever you are working with Python (or any other language) search the internet for functions that might help you, there will almost always be something that does what you want in just one line in Python.

    Example: You have declared a variable and made a for loop to calculate the sum of the heights. This could be solved by playing google on "how do I compute the sum of the elements of a list in python".

    Response: sum(nome_da_lista)

    In your case: media = sum(nome_da_lista)/len(nome_da_lista)

        
    25.05.2017 / 19:50
    2

    First you calculate the average height:

    altura_alunos = [1.70, 2.0, 1.40, 1.55, 1.70]
    media_altura = sum(altura_alunos) / float(len(altura_alunos))
    

    This loop is correct. Just put all the logic together:

    for j in range(len(idade_alunos)):
        if idade_alunos[j] >= 13:
            if altura_alunos[j] <= media_altura:
                x += 1
    
        
    25.05.2017 / 20:12