Python Forum
Need help with my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with my code
#1
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.

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)
Reply
#2
No takers on this in a bit, so will throw in my hat.
Think about what structure you would like to have in order to answer the question. For me, that would be a table with the name, total courses, and sum of grades (will take the sum of the grades, divide by total courses to get the average).
So, would build that structure in a loop.
likes this post
Reply


Forum Jump:

User Panel Messages

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