Python best student lists

Asked

Viewed 78 times

-1

supposing I have x students who have had x notes. how do I put the name of the student who had the best grade?

# coding: iso-8859-1 -*-
import math
nAlunos=0
while True:
    nAlunos=eval(input('Indique o número de alunos:'))
    if 1<=nAlunos<=100:
        break
    else:
        print('O número de alunos tem de ser entre 1 e 100')
        print()
ListaNomes=[0 for i in range(0,nAlunos)]
ListaNotas=[0 for i in range(0,nAlunos)]
reprovados=0
suficiente=0
bom=0
muitobom=0
for i in range(0,nAlunos):
    ListaNomes[i]=input('Nome:')
    while True:
        ListaNotas[i]=eval(input('Nota:'))
        if 0<=ListaNotas[i]<=20:
            if 0<=ListaNotas[i]<10:
                reprovados=reprovados+1
            elif 10<=ListaNotas[i]<14:
                suficiente=suficiente+1
            elif 14<=ListaNotas[i]<17:
                bom=bom+1
            elif 17<=ListaNotas[i]<20:
                muitobom=muitobom+1
            break
        else:
            print('A nota tem de estar entre 0 e 20')
            print() 
PercentagemReprovados=(reprovados*100)/len(ListaNotas)
PercentagemSuficiente=(suficiente*100)/len(ListaNotas)
PercentagemBom=(bom*100)/len(ListaNotas)
PercentagemMuitobom=(muitobom*100)/len(ListaNotas)
print()
for i in range(0,nAlunos):
    print(ListaNomes[i])
    print(ListaNotas[i])
print()
print(PercentagemReprovados)
print()
print(PercentagemSuficiente)
print(PercentagemBom)
print(PercentagemMuitobom)
print()
print(max(ListaNotas))
for i in max(ListaNotas[i]):
    print(ListaNomes[i])
    

the last line is giving me error and I’m not realizing how I should add the name of the student who had the highest grade

1 answer

-1


you need to know the position of the grade list that has the best grade and print the student name in the same position. Just add two lines:

argmax = max(enumerate(ListaNotas), key=lambda x: x[1])[0]
print(ListaNomes[argmax])

there are other ways to do this. If you want to get all the students who got the best grade you can do:

melhor_nota=max(ListaNotas)
melhores = [ListaNomes[i] for i in range(len(ListaNotas)) if ListaNotas[i] == melhor_nota]
  • If you had two students the same grade should appear the two who had the best grade, I do not know if it is possible to appear all the students who had the best but if you can help me thank

  • 1

    I added what to do to have all students with the best grade

  • can you see the result without appearing between [ ]? for example now appears ['Tiago','Ruben'] ... you can only see Tiago , Ruben ?

  • 1

    gives yes, you can do print(str(melhores)[1:-1]) or print(", ".join(melhores)). In the first the names appear in quotes and in the second not

Browser other questions tagged

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