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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert numbers into list lokesh 1 410 Jun-03-2021, 06:37 AM
Last Post: menator01
  adding numbers in a list Nickd12 2 575 Jan-15-2021, 12:46 PM
Last Post: Serafim
  Print variable values from a list of variables xnightwingx 3 700 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  importing a list of numbers into python script barrypyth 8 1,280 Aug-22-2020, 09:10 PM
Last Post: barrypyth
  Print the number of items in a list on ubuntu terminal buttercup 2 749 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Is there a way i print odd and even numbers separately? spalisetty06 5 761 Jul-21-2020, 06:48 PM
Last Post: spalisetty06
  Summing a list of numbers Oldman45 6 835 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 6,049 Jul-01-2020, 03:53 PM
Last Post: ndc85430
  Topic: “Filter numbers with a list comprehension” (PyBite #107) Drone4four 4 844 Jun-11-2020, 08:31 PM
Last Post: Drone4four
  taking input doesnt print as list bntayfur 2 688 Jun-04-2020, 02:48 AM
Last Post: bntayfur

Forum Jump:

User Panel Messages

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