Wrong answer and I don’t know why in Python

Asked

Viewed 143 times

0

I have a doubt in the following question:

Ages and heights of K students have been noted. Make a Program that determine how many students over 13 are below the height of average height of all students. Entry must contain a number N in the first line indicating the number of students in the test case. In followed, each row contains age and height.

My code is like this

idade = []
altura = []
somaAltura = []
cont = 0

qntAlunos = int(raw_input('Informe a quantidade de alunos: '))

for i in range(qntAlunos):
    print 'Informe a idade:'
    idade.append(int(raw_input()))
    print 'Informe a altura:'
    altura.append(float(raw_input()))
    somaAltura = sum(altura)
    media = somaAltura / qntAlunos

for j in range(qntAlunos):
    if altura[j] < media and idade[j] > 13:
        cont += 1


print cont

But because when I run the program, the counter never gives the correct answer?

  • "Then each line contains age and height", you are reading age and height in separate lines.

  • You are calculating the sum of heights and the average and the back of the first one is. It doesn’t affect the result, but it would be more efficient to do this calculation outside the loop. And, as @Andersoncarloswoss said, there’s a problem with reading. idade, altura = raw_input().split() is about what you’ll need.

1 answer

0

Check your counter. You set it to "cont1 = 0" but use "cont += 1". Define it as cont = 0, keeping the same nomenclature of the variable that will work. Hug and good studies.

  • I’m sorry, I forgot to change that because I was running tests to try and find the mistake. In fact, the variable is equal to when I declare and the error persists.

Browser other questions tagged

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