#1

Define a function called get_class_average that has one argument students. You can expect students to be a list containing your three students.
First, make an empty list called results.
For each student item in the class list, calculate get_average(student) and then call results.append() with that result.
Finally, return the result of calling average() with results.

Below are my codes but its not working

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)
Avg=total/len(numbers)
return Avg

def get_class_average (students):
students = [lloyd,alice,tyler]
results = []
def get_average(students):
homework=average(students["homework"])
quizzes=average(students["quizzes"])
test=average(students["tests"])
homework=homework*10/100
quizzes=quizzes*30/100
test=test*60/100
return homework+quizzes+test
results.append(get_average(student))
return average(results)

Welcome to our forums! Please Introduce Yourself
#3

Hi there!

You're on the right track with your code, but I have a few suggestions for you.

• Firstly, in your `get_class_average()` function, we need to declare the `students[]` list outside the function since we need to use it as a parameter.

We can simply add a line that says `students = [lloyd, alice, tyler]` and your function will expect a list of `dicts` that contain the needed variables.

• Also, I can't tell from your code without the indentation, but it looks like you're defining your `get_average()` function inside your `get_class_average()` function. Although we can define functions inside other functions, we shouldn't have to do that unless we're doing something really funky.

So, check your code and see if you have already defined `get_average()` somewhere else in your program. If you have, no need to do it again!

If not, let's restructure your code as such:

``````def get_average(student):

def get_class_average(students):
results = []

It's a good idea to define functions in the order that you use them in your program. For example, `get_class_average()` utilizes the function `get_average()`, so we should define `get_average()` before the other.

Let's take a closer look at your `get_average()` function first:

``````def get_average(students):
homework=average(students["homework"]) #this line is great!
quizzes=average(students["quizzes"]) #so is this one
test=average(students["tests"]) #this one too :slight_smile:

#these lines can be restructured so we get code that does
#more work for us!
homework=homework*10/100
quizzes=quizzes*30/100
test=test*60/100
return homework+quizzes+test``````

The 4 lines in the end of your `get_average()` function work, but consider combining them into one return statement like this:

``return 0.1 * homework + 0.3 * quizzes + 0.6 * tests``

Here, we still accomplish the same thing, but we can utilize the order of operations to simplify our code. So, all together we have:

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

Now, your `get_class_average()` function is on the right track as well, but we need to define it so it iterates over the `students` list for the required variables.

Let's do this with a `for` loop:

``````for student in students:
results.append(get_average(student))``````

This loop will go through the students in the list and add each student's average to the `results` list. After, we can simply `return average(results)` since we've already defined an average function.

After restructuring, we have:

``````students = [lloyd, alice, tyler]

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

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

Even though I provided you the answer, please take the time to understand the points I have given you so you can continue to learn and write even better code!

Hope this helps

#4

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