[Python] Finding out who got the highest mark among the students

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am newbie in python and currently working a project to sort and calculate the marks among the students. I was asked to find out who got the highest mark among the students.

    if only one student, it will output: [name] gets the highest mark. if only two student, it will output: [name] and [name] get the highest mark. if more than two students, it will output:[name], [name] and [name] get the highest marks.

    However, I was stuck at more than two student students and dunno how to meet the above requirement, for example if there is a class with 40students and all of them got the same marks, my program does not work. I thought of recursion but I dunno its a good direction or not.

    Here is what I got now:

    marks = {'Sally': 65, 'Ken': 61, 'Michael': 88, 'Yan': 67, 'Mary': 88,'May': 88}
    highest = max(marks.values())
    n = [k for k, v in marks.items() if v == highest]

    if len(n)==1:
    print(n[0],' got the highest mark')
    elif len(n)==2:
    print(n[0],'and',n[1],' got the highest mark')
    elif len(n)==3:
    print(n[0],',',n[1],'and',n[2],' got the highest mark')

    Continue reading...

Compartilhe esta Página