Wanted to check if the gradebook code is correct

here is the code:


Bob = {"name": "bob", "assigbment": [48, 39, 98, 87],
       "presentation": [78, 78, 89, 78], "lab_task": [67, 89, 89, 90]}
Jay = {"name": "bob", "assigbment": [48, 39, 98, 87],
       "presentation": [78, 78, 89, 78], "lab_task": [67, 89, 89, 90]}
# print out info
print(Bob, Jay)


def average(marks):
    total_sum = sum(marks)
    total_sum = float(total_sum)
    return total_sum / len(marks)


def get_average(student):
    ass_avg = average(student["Assignment"])
    pre_avg = average(student["presentation"])
    lab_avg = average(student["lab_task"])
    weight = ass_avg * 0.6 + pre_avg * 0.1 + lab_avg * 0.3
    return weight


def letter_grade(weight):
    if weight >= 90:
        return ("HD")
    elif weight >= 80:
        return ("D")
    elif weight >= 70:
        return ("CR")
    elif weight >= 60:
        return "P"
    else:
        return ("f")


students = [Bob, Jay]
results = []

def class_average(students):
    results.append(get_average(students[0]))
    results.append(get_average(students[1]))
    class_avg = average(results)
    return class_avg

Do you have a question? Is your program doing what you expect it to?

A good way to check that the parts work correctly is to write tests: unittest — Unit testing framework — Python 3.11.0 documentation.

1 Like

When you say “correct”, correct in what way?

Right now, none of the functions are being used (all of which should be placed ahead of any of the main code). Also, the way this seems to be panning out, you’ll need to edit the code in order to add more students. Would it not be better to have an app that was not so high maintenance?

Also, simply printing the dictionaries is not so pretty, so the data presentation could use some work.

I’ve not checked any of the math.

My comments are meant as constructive criticism, not a knock down, so I trust that they’ll be taken in the intended way.

b.t.w: Jay and Bob are the same; a simple C&P error?

alright thank you ill work on it

I think (and please correct me if I’m wrong) the reason that you’re adopting an “edit the code” approach for saving data, is that you’re unsure of how else to save the data?

If that’s the case, then you could use a technique that I’ve used to solve the same issue: shelve — Python object persistence — Python 3.11.0 documentation

It’s a very light weight solution with which to simply save the current sate of an object (or objects) such as a dictionary, which can be restored as and when the app is once again run.


To add…

Is this the kind of base app that you have in mind?

def new_grades(entry):
    grades = []
    grade = True
    print(f"Enter grades for {entry} (an empty entry to finish)")
    while grade:
        grade = input(": ")
        if grade:
            grades.append(int(grade))
        else:
            break
    return grades


grade_book = {}
quit = False
while not quit:
    names = grade_book.keys()
    name = input("Name (quit to exit): ")
    if name == 'quit':
        break
    else:
        if name in names:
            print(f"Grades for {name}:")
            print(f"Assignment: {grade_book[name]['assignment']}")
            print(f"Presentation: {grade_book[name]['presentation']}")
            print(f"Lab task: {grade_book[name]['lab_task']}")
        else:
            print(f"New entry: adding {name}")
            grade_book[name] = {
                'assignment': new_grades("Assignment"),
                'presentation': new_grades("Presentation"),
                'lab_task': new_grades("Lab task")
            }

# this is to simply show that the dictionary has indeed been updated
print("\nexit\n")
print(grade_book)
# the grade book could be saved using the technique that I've suggested

It’s far from a complete solution, with no error checking, but more a simple proof of concept.

If this is along the lines that you have in mind and need help to further develop, then that’s fine. If not, well, that’s fine also, but at the very least I hope that it serves as some sort of a guide (maybe).

1 Like

thanks for the help im new to all this doing school for this part time, trying to do it in my spare time after my full time work in a different career thats why its a little bit hard for me unfortunately :frowning:

It not easy for anyone, at the start, but over time, you’ll learn more and it becomes easier to see solutions, based on the knowledge and experience you gain. The only way to learn Python, is to use Python.

I didn’t intend to discourage you and I hope I’ve not done so.

The code that you posted is 99% unused. This is the only part that does anything, as is:

Bob = {"name": "bob", "assignment": [48, 39, 98, 87],
       "presentation": [78, 78, 89, 78], "lab_task": [67, 89, 89, 90]}
Jay = {"name": "bob", "assignment": [48, 39, 98, 87],
       "presentation": [78, 78, 89, 78], "lab_task": [67, 89, 89, 90]}
# print out info
print(Bob, Jay)

If what I’ve posted is what you have in mind and you want to incorporate the functions that you have written, then I see no reason that it can’t be done.

That said, I’ve not gone deep into this, as I was unsure on how you wanted to proceed.

nah im not discouraged and i appreciate the help

1 Like

Good to know.

You clearly have some experience on which to build, so I’m sure that you can do this. If you need help (which we all need, from time to time), just ask.

Try to post code that, for the better part, is working, then say what it’s not doing, but you want it to do. That way it’s more progressive.