So the goal here for me is to take three dictionaries, and print it off in this format:
Name: "Name"
Homework: "Average of list"
Quizzes: "Average of list"
Tests: "Average of list"
I have the 3 dictionaries included in a list and I'm having trouble digging down. I'm trying to go step by step here.
So after viewing this code:
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
students = [lloyd,alice,tyler]
def compute_grades(ourstudents):
for student in ourstudents:
print "Name: " + student["name"]
print "Homework: ", sum(student["homework"]) / len(student["homework"])
print "Quizzes: ", sum(student["quizzes"]) / len(student["quizzes"])
print "Tests: ", sum(student["tests"]) / len(student["tests"])
compute_grades(students)
Is there a way I could just do a basic check so this will work on ANY dictionary?
For instance...
- Is the key definition a string? If so, print it off!
- Is the key definition a list? If so, run this averaging function and print off the result.
I basically just want to minimalize the hard coding in this and come up with a more elegant solution. Any help is appreciated!