8. Part of the Whole

#1

Hi there.

I don’t get how to get rid of this error:

get_class_average([alice]) resulted in an error: maximum recursion depth exceeded

``````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]
}

def average(numbers):
total = sum(numbers)
total = float(total)
result = total / len(numbers)
return result

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
return 0.1 * average(student["homework"]) +  0.3 * average(student["quizzes"]) + 0.6 * average(student["tests"])

float(score)
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"

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

#2

A function calling itself is recursion

In your case, inside get_class_average function you call get_class_average, this will keep going until the maximum depth (amount of time a function can call itself) is reached

sort of like an infinity loop, but with functions and recursion

#3

Thanks for your good explanation. Now I see, but still don’t get how can I append then the function to have the results?

#4

we have `get_average()` to calculate the average grade of a single `student`, we get the individual student due to the loop, so we can use get_average as a helper function in get_class_average

#5

Thanks.

#6

But how are you going to learn this thought process? Like: get_class_average is clearly not the right function to call, why? What i am trying to achieve here? Which function would this require and so forth?

Because now i did this step for you

#7

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