Part of the whole


I'm getting the following Error Code:
Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected

Here is my code:
def get_class_average(students):
results = []
for group in students:
return get_average(group)
return results.append(get_average(group))
return average(results)

What is wrong with my code?


You will find me harping constantly about using variable names that are truthful and describe what they represent. The key variable in this loop will be a student dictionary, in the singular. Granted the dictionary contains several items, but even they are not represented by any form of grouping, hence not a group. This variable name is erroneous and misleading.

Why not,

for student in students:


This makes much more sense to the reader. For singular in plural. How hard could that be to understand right off?

You will find that this goes a long way in helping to debug your code. You are, after all, the principal reader. Make it easy for yourself, keeping the rest of the reader community in mind.

The singular - plural connection is fairly easy to make in English though there are some failing points.

for deer in deer:

for bison in bison:

for moose in moose:

for sheep in sheep:

Not as handy as,

for mouse in mice:

for goose in geese,

for die in dice:

In some situations it will be tricky to come up with the variable name combo, but not often, and there is always another way. We just need to keep it sensible and comprehensive.


So I updated my code to the following:

def get_class_average(students):
results = []
for student in students:
return get_average(student)
return results.append(get_average(student))
return average(results)

But I am still getting the same error message:
Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected


That line can be removed.

Drop the return keyword on the next line, and only return on the last line, and outside of the loop block.


Thank you so much! I fixed it!!


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