6.11 Just Wait and See - list indices must be integers, not str


#1

Hi all,

Can you please help me see what's wrong? The console says there are no errors, but Codecademy keeps giving me the error: "Oops, try again. get_average(alice) raised the following error: list indices must be integers, not str"

<>
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 average(numbers):
total = sum(numbers)
total = float(total)
total /= len(numbers)
return total
total = float()/2

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(students["tests"])

return homework * .1 + quizzes *.3 + tests *.6

</>


#2

I kept getting all kinds of errors and then I did this:

def average(numbers):
total = sum(numbers)
total = float(total)
return total / len(numbers)
def get_average(student):
homework = average(student["homework"])*0.1,
quizzes = average(student["quizzes"])*0.3,
tests = average(student["tests"])*0.6,
total = homework + quizzes + tests
return sum(total)


#3

Thanks! I tried that also, but nothing seemed to work. I ended up just copying code from another thread.


#4

Hi @hypnopilot,

I see you have already tried quite a few approaches, so chipping in may be of help.

# Add your function below!
def average(numbers):
    total = sum(numbers)
    total = float(total)
    result = total/len(numbers)
    return result
    
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

Cheers


#5

well i face same problem and i figured you have to put comma , after variable homework quizzes and tests . may be it is too late but just in case you did not know it yet


#6

There seems to be no set ways for this code to pass. My code ended up passing looking like this:
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 average(numbers):
total = sum(numbers)
total = float(total)
total = total / len(numbers)
return total
def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
total = homework + quizzes + tests
return 0.1 * homework + 0.3 * quizzes + 0.6 * tests


#7

You setup the values of homework, quizzes, and tests nicely but failed to place those values in the return function. I understand where you were coming from since you defined all 3 variables but in the return function, you nned to list everything out.

return average(student["homework"]) etc.


#8

I did it like this:

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
homework = homework*0.10
quizzes = quizzes*0.30
tests = tests*0.60
return homework+quizzes+tests

Not the most efficient program but it still works.


#9

hi all it just we must return a value for a function otherwise it pops up like I guess..


#10

your problem was the word "student" and "students". you need to be consistent- use only student or students. if you use "student" it has to be previously define


#11

So I don't know why I am getting this error: Oops, try again. get_average(alice) raised the following error: 'float' object is not iterable

Here is my code, where am I going wrong?

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

def get_average(student):
homework = sum(student)
return float (homework) / len(homework)

def get_average(student):
quizzes = sum(student)
return float (quizzes) / len(quizzes)

def get_average(student):
homework = sum(student)
return float (tests) / len(tests)

def get_average(student):
homework = average(student["homework"]) *.1
quizzes = average(student["quizzes"]) *.3
tests = average(student["tests"]) *.6
return sum (homework + quizzes + tests)


#12

how do i fix indentation in this comment box thing?