1

I'm trying to get the person who did the most course into my summary function. I found that the way to access those elements is using for key, values in students.items(). When I used print(len(values)), I found that it printed out the amount of courses of each student did 5,0,4 but I'm having trouble only printing out the highest number of courses done and making sure the student's name is printed along with it. I tried to use max() but got int object is not subscriptable.

from operator import itemgetter

def add_student(students,name):
    students[name] = set()

def print_student(students, name):
    
    if name not in students:
        print(f"{name} does not exist in the database")
    else:
        print(f"{name}:")
        print(f"Number of completed courses: {len(students[name])}")
        for course in students[name]: #printing out the contents of the tuple
          print(f"{course[0]} ({course[1]})")
        total_score = 0
        for course in students[name]:   
            total_score += course[1]
            
        try:
            print(f"average grade : {total_score/len(students[name])}")
        except ZeroDivisionError:
            print("no completed courses")
                 
    
def add_course(students,name, course:tuple):
    if course[1] == 0:
        return 0
    students[name].add(course)
    
def summary(students):
    new_set = ()
    print(f"students {len(students)}")
    for key, values in students.items():
        print(len(values))
    
        
    
        

 
   
students = {}
add_student(students, "Peter")
add_student(students,"Kristina")
add_student(students, "Eliza")
add_course(students, "Peter", ("EECS 2011", 70))
add_course(students, "Peter", ("EECS 1015", 52))
add_course(students, "Peter", ("EECS 245", 42))
add_course(students, "Peter", ("EECS 2412", 52))
add_course(students, "Peter", ("EECS 2030", 53))
add_course(students, "Eliza", ("EECS 43", 3))
add_course(students, "Eliza", ("EECS 52", 72))
add_course(students, "Eliza", ("EECS 43", 72))
add_course(students, "Eliza", ("EECS 74", 72))
print_student(students, "Peter")
print("--------------")
print_student(students,"Eliza")
print("------summary here-------")
summary(students)
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
  • 1
    Why does `add_course` return 0 sometimes? I'd recommend also doing some validation to ensure that the `course` tuple is in the correct format. I'd also suggest wrapping all this functionality in a class, and learn about OOP! – ddejohn Apr 26 '22 at 16:46

4 Answers4

3

You can take the maximum of the keys of the students dictionary, using the length of the corresponding value (i.e. number of courses) as the key to compare on:

def summary(students):
    student_with_most_courses = max(students.keys(), key=lambda x: len(students[x]))
    print(student_with_most_courses)

This outputs:

Peter
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
  • 2
    Potentially worth noting that if there are multiple students which have done the maximum number of classes then this will only print one of them (although it isn't clear what the OP wanted in this case) – Cameron Ford Apr 26 '22 at 16:23
  • You're right. I'm happy to write that in if OP needs it. – BrokenBenchmark Apr 26 '22 at 16:23
  • A slight alternative to get the student and the number of courses in one go: `student, number = max(((s, len(c)) for s, c in students.items()), key=lambda t: t[1])` – Anakhand Jan 10 '23 at 09:46
1

you can short the names on bases of the no of courses and then from there take the result.

def summary(students):
    new_set = ()
    print(f"students {len(students)}")

    result =  sorted(students.items(), key =lambda x:len(x[1]),reverse=True )
    for student_name, course in result:
        print(student_name, len(course))

output

average grade : 54.75
------summary here-------
students 3
Peter 5
Eliza 4
Kristina 0 
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
0

I think this question was discussed beforely; How do i sort a dictionary by his value? and How to get the 3 highest value items from dictionary?.

def summary(students):
    [print(students.get(courses), courses) for n_courses in sorted({name:len(students[name]) for name in students}) for courses in n_courses]
user11717481
  • 1
  • 9
  • 15
  • 25
0

Code of summary, almost done!

`
def summary(students):
result = [[key, len(values)] for key, values in students.items()]
result.sort(key=lambda row: row[1], reverse=True)

print(f"students: {len(students)}")

for name, number in result:
    print(name, number)
`