Apr-25-2022, 12:57 AM
(This post was last modified: Apr-25-2022, 12:57 AM by NeedHelpPython.)
For my summary function, what I want it to complete is to get the student with the most courses completed and the highest average. I am having trouble on figuring out how to access the elements in order to solve this.
So for the summary, most courses completed would be Peter with 3 courses and the best grade average would be Elizia with 4.5.
So for the summary, most courses completed would be Peter with 3 courses and the best grade average would be Elizia with 4.5.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
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): print ( f "students {len(students)}" ) students = {} add_student(students, "Peter" ) add_student(students, "Eliza" ) add_course(students, "Peter" , ( "Data Structures and Algorithms" , 1 )) add_course(students, "Peter" , ( "Introduction to Programming" , 1 )) add_course(students, "Peter" , ( "Advanced Course in Programming" , 1 )) add_course(students, "Eliza" , ( "Introduction to Programming" , 5 )) add_course(students, "Eliza" , ( "Introduction to Computer Science" , 4 )) print_student(students, "Peter" ) print_student(students, "Eliza" ) print ( "------summary here-------" ) summary(students) |