Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print max numbers in a list
#1
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)


Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print variable values from a list of variables xnightwingx 3 335 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  importing a list of numbers into python script barrypyth 8 529 Aug-22-2020, 09:10 PM
Last Post: barrypyth
  Print the number of items in a list on ubuntu terminal buttercup 2 426 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Is there a way i print odd and even numbers separately? spalisetty06 5 368 Jul-21-2020, 06:48 PM
Last Post: spalisetty06
  Summing a list of numbers Oldman45 6 407 Jul-12-2020, 05:30 PM
Last Post: Oldman45
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 6 2,927 Jul-01-2020, 03:53 PM
Last Post: ndc85430
  Topic: “Filter numbers with a list comprehension” (PyBite #107) Drone4four 4 399 Jun-11-2020, 08:31 PM
Last Post: Drone4four
  taking input doesnt print as list bntayfur 2 354 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  Supposed to print out even numbers DallasPCMan 4 355 May-21-2020, 05:50 PM
Last Post: ndc85430
  Taking brackets out of list in print statement pythonprogrammer 3 578 Apr-13-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020