# Could you please tell me why this code shows an error?

#1

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)

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

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):
class_list = [‘lloyd’ , ‘alice’ , ‘tyler’ ]
result = []
for student in class_list:
get_average(student)
result.append(get_average(student))
return average(result)

#2

Some statements within your `get_class_average` function need explanation, for example this one …

``````  class_list = [‘lloyd’ , ‘alice’ , ‘tyler’ ]
``````

Consider the role of that function’s `class_list` parameter.

#3

Could you please elaborate it? I didn’t got that.

#4

When the `get_class_average` function is called, an argument is passed to it, and within the function, the value or a reference to that argument is represented by the function parameter, which is `class_list`. However, in the first line of the function, you assigning another `list` to `class_list`, thereby losing the reference to the argument, presumably a `list`, that was originally passed to the function.

#5

But that’s what we are supposed to do. we are supposed to find class average of lloyd alice and tyler .So defining class_list equal to that won’t make a difference right?

#6

These statements, outside the function, should do that …

``````students = [lloyd, alice, tyler]
print get_class_average(students)
``````

Do not place quotes around the names of the variables.

As the function executes, `class_list` will represent `students`. However, you must remove this statement from the function, so that we do not lose the option of processing a different `list` by passing it to the function …

``````class_list = [‘lloyd’ , ‘alice’ , ‘tyler’ ]
``````

The function is not very useful if it can only process a class with `lloyd`, `alice`, and `tyler`.

#7

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