1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] MOOC - Students database

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    I'm new in python and I'm using MOOC course to learn (and Udemy). I come across this exercise and I'm stuck with this:

    Students database exercise - Part 3

    I wrote this code (unfinished!), but im having problems with the add_course function. I am not able to ignore adding courses with lower grade. I tried for loops too.

    Can anyone help me here? Thanks a lot!

    # Write your solution here
    def add_student(database: dict, name: str):
    courses = []
    database[name] = courses

    def add_course(database: dict, name: str, course: tuple):
    courses = database[name]
    if course[1] == 0:
    pass
    elif course[0] in courses:
    pass
    else:
    courses.append(course)

    def print_student(database: dict, name: str):
    if name not in database:
    print(f"{name}: no such person in the database")
    else:
    if len(database[name]) == 0:
    print(f"{name}:")
    print(" no completed courses")
    else:
    completed_courses = len(database[name])
    sum_grades = 0
    print(f"{name}:")
    print(f" {completed_courses} completed courses:")
    for courses in database[name]:
    sum_grades += courses[1]
    avg_grade = sum_grades / len(database[name])
    print(f" {courses[0]} {courses[1]}")
    print(f" average grade {avg_grade}")

    #def summary(database: dict):
    # pass

    if __name__ == "__main__":
    students = {}
    add_student(students, "Peter")
    add_course(students, "Peter", ("Introduction to Programming", 3))
    add_course(students, "Peter", ("Advanced Course in Programming", 2))
    add_course(students, "Peter", ("Data Structures and Algorithms", 0))
    add_course(students, "Peter", ("Introduction to Programming", 2))
    print_student(students, "Peter")


    Tried: For loops, appending to another list first.

    Expected: If the course is already in the database in that specific student's information, the grade recorded in the database should never be lowered if the course is repeated.

    Resulted now: I'm getting duplicates.

    Continue reading...

Compartilhe esta Página