Part of the Whole--Anyone help


#1

I got this error and no clue what was it
"Oops, try again. get_class_average([alice]) resulted in an error: 'float' object has no attribute 'append'."

My earlier code was

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

and I changed it to "results.append(get_average(student))" and is working fine.
what is the difference. can anyone help?


#2

you declare a list:

results = []

to which you can append the average result of each student. Which is good, but then here:

results = get_average(student)

you re-assign results, now it is a float (decimal number) and then you try to append to a float. That doesn't work, .append() is a method for appending to lists, it doesn't work for floats.

You could do your approach:

x = get_average(student)
results.append(x)

just don't overwrite the list with a float


#3

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