8. Part of the Whole


#1

Hi. It has been a while since I have programmed in Python, and I forgot a few commands. I did this code, but can't find the problem. The error message is:

File "python", line 54
get_average(alice)
^
SyntaxError: invalid syntax

Can anyone help?

def get_class_average(students):
        results = [
        get_average(lloyd)
        get_average(alice)
        get_average(tyler)
        ]
        results.append()
return results.average()

#2


If you read instructions ,It has said to define an empty list results

How to define an empty list ?

if I had to define an empty list x..I'll do something like this

x = []

update**
After you debug it..
There are more errors to come though.


#3

Ok, I fixed it a bit:

def get_class_average(students):
        results = []
        results.append()
        return results.average()

Error:
Oops, try again. get_class_average([alice]) resulted in an error: append() takes exactly one argument (0 given)


#4

Hint**
Can you figure out what they are saying to do?

For each student item in the class list, calculate get_average(student) and then call results.append() with that result.

something missing there...
for each...


#5

So... they are asking for a for student in students loop to preform this function on all the students individually, rather than all together?


#6

Aye! you're getting there!!

don't forget to indent your next block properly


#7

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

Error:
Oops, try again. get_class_average([alice]) resulted in an error: 'list' object has no attribute 'average'
If this is repetitive, I apologize, it has been about 2 weeks since I was last on.


#8

lets get to instruction one more time...

For each student item in the class list, calculate get_average(student) and then call results.append() with that result.

Notice... with that result.


#9

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

It tells me get_class_average([alice]) should return a number.
In the window it says 'A'


#10

this means, inside the loop you need the append get_average(student) to results

then after the loop, return the average of your results list

our get_class_average function should calculate the class average, to achieve this we first need to calculate the average grade (using get_average) for each student in students, this grades we store in a list

now that we have a list with each student grade, we just need to calculate the average of this lists to calculate the class average


#11

@toddhahnsolo as @stetim94 mentioned!


#12

This works:

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

Thanks!


#14