Problem with lists - Python

Asked

Viewed 1,123 times

0

I can’t do:

  1. Ages and heights of 5 students were noted. Do a Program to determine how many students over 13 are tall lower than the average height of such pupils.

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)
  • 6

    Start by studying algorithms. Clearly you have a lot of difficulty in structuring any desired logic. Then calculate the average height.

2 answers

4


Do things step by step. Try to isolate problems.

First thing, the problem says it will be necessary to use the height average, so first thing, calculate the height average and store in a variable.

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

Once this is done, just use the iterated element’s Input to find its corresponding height and check if it is smaller than the average. To find the corresponding height you can use alturaAlunos[idadeAlunos.index(elemento)] where element will be the element being iterated in the loop.

Last hint, use a loop for to iterate on the list idadeAlunos.

Post your code in case you’ve managed to solve the problem, if you can’t, post so I can help you solve.

Whenever you’re working with Python (or any other language) search the internet for functions that can help you, you’ll almost always have something that does what you want in just one line in Python.

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

Answer: sum(nome_da_lista)

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

  • 1

    I modified my code above, I’d done it before, and I’m stuck on that part.

  • Cool, I can tell you’re trying and you’re on the right track. In your last loop, you are iterating over the old list of students and assigning the index of each student in the variable j at each iteration. As the lists are listed by position, you can do the following: in your if command, let it more "complete" to satisfy both conditions as follows: if idadeAlunos[j] > 13 and alturaAlunos[j] < x: contador = contador + 1 Do not forget to declare the counter variable with 0 out of loop

  • 1

    Yes, I understand that it can be resolved with ready-made functions. But it’s what I need to do with repeating structures, because that’s what’s gonna fall on my test today, hahaha

  • So guys, I’m stuck on this part, definitely... I know what you have to do, but I don’t know what to use to execute... Code above

  • Kkkkkkk, forget it, guys, I got it, I was hot-headed, thank you all!

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
  • In that case, the float the length of the list would be unnecessary, wouldn’t it? Since sum will already return a float, would be sharing a float/int, that is valid.

  • 1

    Only by operator orthogonality. Yes, it can be taken.

Browser other questions tagged

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