Oops, try again. get_class_average([alice]) resulted in an error: unsupported operand type(s) for +: 'float' and 'dict'


#1

hello all,
please help me with this error...

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

Add your function below!

def sum(numbers):
total = 0.0
for n in numbers:
total = total + n
return total

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

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

def get_letter_grade(score):
if score >= 90:
return "A"
elif score >= 80 and score < 90:
return "B"
elif score >= 70 and score < 80:
return "C"
elif score >= 60 and score < 70:
return "D"
else:
return "F"
get_letter_grade(get_average(lloyd))

def get_class_average(student):
return average(student)

results = []
results.append(get_average(lloyd))
results.append(get_average(alice))
results.append(get_average(tyler))

get_class_average(results)


#2

anybody??? please help. I am stuck here :confused:


#3

i think you're missing the print function
you're calling the functions, and they are returning a value properly, but you're not doing anything with the returned values

get_letter_grade(get_average(lloyd))

and

get_class_average(results)

add print in front of them, that might help.

i looked at the lesson again, ignore the above part.. lol

Oops, try again. get_class_average([alice]) resulted in an error: unsupported operand type(s) for +: ‘float’ and ‘dict’

which lesson are you on?

i think i get it now, its hard to read code without the proper indentation, anyways, your last function, its not properly set up

def get_class_average(student):
    return average(student)

    results = []
    results.append(get_average(lloyd))
    results.append(get_average(alice))
    results.append(get_average(tyler))

get_class_average(results)

Instructions
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.
2. First, make an empty list called results.
3. For each student item in the class list, calculate get_average(student) and then call results.append() with that result.
4. Finally, return the result of calling average() with results.

your return function is too early in the code, messing up what should be returned


#4

Hi,
Sorry for the indentation :stuck_out_tongue: but... It is all correct I guess the list "results" is not in the function as you showed in your example...

Please excuse the indentation again... :wink:

But it is something like this...


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


get_class_average(results)
:confused:


#5

you're code would be okay,but the site is expecting a different kind of code,
the site tests your get_class_average() function, one of the tests your function with is get_class_average([alice])

The site expects your function to take the input as a list containing Dictionaries (i.e, lloyd, alice, tyler).
but your code already gets a student's average and puts it in the list so instead of a list like [lloyd, alice, tyler]
you are putting in [88.4,95.4,78.2] into get_class_average(), which is fine, if you were coding for yourself, but as i said the site expects a certain kind of code, it expects a list of dictionaries

The site breaks your code because its invoking your function with [alice], and your function invokes average([alice]), your average puts [alice] into sum([alice])
at this point, your data should look something like this
[100.0, 92.0, 98.0, 100.0]

but with your code, the way the site test's your function, your data looks like
{'tests': [89.0, 97.0], 'homework': [100.0, 92.0, 98.0, 100.0], 'name': 'Alice', 'quizzes': [82.0, 83.0, 91.0]}
at this point.

hope that helps


#6

i have results as a list for the three students
can't I feed that into the average(results) function?


#7

you'll have to code it to take in a list with objects in it.
get_class_average([alice]) <----you'll have to get this to work, because that's how the site tests your code.


#10