Doubt in my code is not printing correctly

2

question: Using the text file notas_estudantes.dat write a program that calculates the minimum and maximum grade of each student and print the name of each student along with its maximum and minimum mark. file:

jose 10 15 20 30 40
pedro 23 16 19 22
suzana 8 22 17 14 32 17 24 21 2 9 11 17
gisela 12 28 21 45 26 10
joao 14 32 25 16 89

My code:

arq=open('notas_estudantes.dat','r')
conteudo=arq.readlines()
arq.close()
for item in conteudo:
    lista=item.split()
    lista.sort()
    print(lista[-1],':','Nota Maxima:',lista[-2],'Nota Minima:',lista[0])

When I go to print give this:

jose : Nota Maxima: 40 Nota Minima: 10
pedro : Nota Maxima: 23 Nota Minima: 16
suzana : Nota Maxima: 9 Nota Minima: 11
gisela : Nota Maxima: 45 Nota Minima: 10
joao : Nota Maxima: 89 Nota Minima: 14

the others are all correct but when the part of suzana arrives it does not give the correct result, I would like to know if it is something pyhton error or it was myself that I did something wrong in the code.

    
asked by anonymous 07.10.2018 / 03:56

3 answers

3

As said before, you have to be careful about the types you are handling in your containers. I made another implementation to generate the expected result for you. I removed the names, and converted the remainder to integers, making it easier to remove the max and min from the list.

arq = open('arquivo.dat','r')
conteudo = arq.readlines()
arq.close()
for item in conteudo:
    lista = item.split()
    aluno = lista[0]
    del lista[0]
    notas = list(map(int, lista))
    print('Aluno: {}, Nota max: {}, Nota Min: {}'.format(aluno, max(notas), min(notas)))
    
07.10.2018 / 04:46
4

Your code that is wrong, when you call the sort() method, it sorts the list incrementally, but the data in the list returned by the split() method is string , that is, when the sort() method is invoked it checks caractere por caractere of the data, see:

>>> conteudo = "suzana 8 22 17 14 32 17 24 21 2 9 11 17"
>>> dados = conteudo.split(" ")
['suzana', '8', '22', '17', '14', '32', '17', '24', '21', '2', '9', '11',
>>> dados.sort()
['11', '14', '17', '17', '17', '2', '21', '22', '24', '32', '8', '9', 'suzana']

Got it? '11' comes before "2" because it checks caractere por caractere . What you could do to resolve is:

arquivo = open("notas_estudantes.dat", "r")
conteudo = arquivo.readlines()
arquivo.close()
for item in conteudo:
    lista = item.split()
    nome = lista.pop(0)
    lista.sort(key = int)
    print(nome, ":", "Nota Máxima:", lista[-2], "Nota Minima:", lista[0])
    
07.10.2018 / 04:38
3

You are handling notes as strings , so when you do sort, Python places "1", "10", in front of "2", "4" and so on. In this case the best solution is to work with them as a numeric format.

# ...
for item in conteudo:
    lista = item.split()  # converte a linha lida em uma array
    nome = lista.pop(0)  # retira o nome de dentro do array
    notas = [int(i) for i in lista]  # cria um novo array com o que sobrou de
                                     # 'lista', convertendo cada elemento em um
                                     # número inteiro.

    # por fim, usa min() e max() para obter os valores agora que eles são números.
    print(nome, ':', 'Nota Maxima:', max(notas), 'Nota Minima:', min(notas))

Of course this is ONE solution, but as you want to know the maximum and minimum marks (either treat them as values) I preferred the more "numeric" approach.

    
07.10.2018 / 04:52