# Trouble with averages function in 5/9 of student becomes the teacher

#1

I’m on 5/9 “It’s ok to be average”

And I’m sure I’m not formatting this post properly, but I can’t figure out this question AT ALL.

So I can make it do this:
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]
}

print(lloyd[“name”],sum(lloyd[“homework”])/len(lloyd[“homework”]),sum(lloyd[“quizzes”])/len(lloyd[“quizzes”]),sum(lloyd[“tests”])/len(lloyd[“tests”]))

print(alice[“name”],sum(alice[“homework”])/len(alice[“homework”]),sum(alice[“quizzes”])/len(alice[“quizzes”]),sum(alice[“tests”])/len(alice[“tests”]))

print(tyler[“name”],sum(tyler[“homework”])/len(tyler[“homework”]),sum(tyler[“quizzes”])/len(tyler[“quizzes”]),sum(tyler[“tests”])/len(tyler[“tests”]))

<In what way does your code behave incorrectly? Include ALL error messages.>

I get:
(‘Lloyd’, 88.5, 74.0, 82.5)
(‘Alice’, 97.5, 85.33333333333333, 93.0)
(‘Tyler’, 46.0, 51.0, 100.0)

Which is kind of what I want.

<What do you expect to happen instead?>

But I think this is really clunky, and not at all what the question is asking for, and thus not helpful for 6/9 when you go to weight the averages. Any help for a newbie like me would be appreciated.

``````

``````

#2

you should really use a `for` loop to shorten the code quit a lot

you know how to use for loop?

#3

We write simple helper functions so the above can be avoided.

Eg.

Average of a list of grades

``````# grades is a list

``````

Weighted averages

``````# student is a dictionary

def get_average(student):
result = [average(student['homework']) * 0.1]
result.append(average(student['quizzes']) * 0.3)
result.append(average(student['tests']) * 0.6)
return sum(result)
``````

The above is a variation on what is asked in the instructions but the result is as expected.

Now if you wish to generate a report on all the students

``````# print student report

def student_report(student):
print "Name: %s  Final grade: %.2f" % \
(student['name'], get_average(student))
for key in ['homework', 'quizzes', 'tests']:
print "  %s:  %.2f" % (key, average(student[key]))

for student in students:
student_report(student)
``````
``````Name: Lloyd  Final grade: 80.55
homework:  88.50
quizzes:  74.00
tests:  82.50
homework:  97.50
quizzes:  85.33
tests:  93.00
homework:  46.00
quizzes:  51.00
tests:  100.00
``````

As for your dictionaries, the quotation marks are non-standard and will raise a syntax error. Use `''` or `""`.

#4

working on teaching myself for loops now actually. Thank you for the suggestion.

#5

haven’t loop already be covered in the course? You can always revisit those exercises if you need, using a loop would save you a lot of work

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.