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)