Python Forum

Full Version: Print max numbers in a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So, guys, i'm having a problem with my code. What i need to do is take grades of n students and put on a list, and take their grades e put on otherlist what i did, but, i'm not getting print the names of the students if more than one get the highest grade. My code until now:
student = []
grade = []
maxStudents =[]

while len(student) < 5:
    student.append(input('Write de student name'))
    grade.append(int(input('Write the student grade')))

for i in grade:
    
    if i == max(grade,key=int):
        maxStudents.append(student[grade.index(i)])
        

print(maxStudents)
There are many solutions, knowing that ".index(i)" will give you the first occurence only.
Two suggestions:
1) Start an independent counter incremented in each loop. student[x], will give the different names (2 extra lines)
2) Do a diffenrent kind of loop , like "for i in range(len(grade)):", again student[i] will do the trick. (no extra lines)
Paul