Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Top 3 students
#3
Just a comment: You sort the values but the name list is still untouched so the names and values in the result are no longer connected. You get the three highest values but the last three names. The output ought to be:
Top 1: Karen 92
Top 2: Josh 90
Top 3: Amin 81

which can be achieved by sorting the dictionary by value (or by item, which I think is the terminology here, but I'm far from sure). Use sorted and provide a key function (which has to be a lambda function as it will be "in-line"):
scores = {'Josh': 90, 'Adam': 72, 'Amin': 81, 'Narnia': 56, 'Natalie': 65, 'David': 68, 'Karen': 92, 'Elsa': 75}
scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)
for i in range(3):
    print(f"Top {i+1}: {scores[i][0]} {scores[i][1]}")
banidjamali likes this post
Reply


Messages In This Thread
Top 3 students - by banidjamali - Jan-17-2021, 09:08 AM
RE: Top 3 students - by buran - Jan-17-2021, 09:30 AM
RE: Top 3 students - by Serafim - Jan-17-2021, 09:49 AM
RE: Top 3 students - by Serafim - Jan-17-2021, 10:04 AM
RE: Top 3 students - by banidjamali - Jan-17-2021, 11:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python program that calculat grades of students biligorm 3 2,301 Apr-04-2020, 10:11 AM
Last Post: buran
  python dictionary for students records atux_null 12 19,358 Nov-12-2017, 04:47 PM
Last Post: atux_null

Forum Jump:

User Panel Messages

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