Error Student becomes the teacher 6/9


#1

Oops, try again. get_average(alice) raised the following error: global name 'quizzes' is not defined

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

def get_average(student):
homework = average(student["homework"])
homework = average(student["quizzes"])
homework = average(student["tests"])
score1 = homework * 10
score2 = quizzes * 30
score3 = tests * 60
score = score1 + score2 + score3
return score


#2

Second block should look 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
score = homework + quizzes + tests
return score


#3

I prefer to have all the math done on one line and just return that value instead of declaring more variables than we need.

My Code:

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

#5

Can somebody please help me understand what this part does exactly?

I understand what len() does, but how does this mathematically help determine the score?

I am also confused about how people thought to include this part, as it isn't included in the instructions.

"#Inside that function, call the built-in sum() function with the numbers list as a parameter. Store the result in a variable called total. use float() to convert total and store the result in total"


#6

Think of len(numbers) as a variable. If you run that all by itself you'll get a number, so using the len() method this way (someone PLEASE correct me if im wrong) is more dynamic; so if len(numbers) ever does change its value you wont need to rewrite any code. As for how/why this block of code was included, im just as confused as you are, however, it does force you to think outside the box and solve problems yourself. There's more than one way to skin a cat/code out a particular problem.


#7

I'd coded the exact same way and i still get an error. I cannot understand why. Can someone please help me understand this? Thanks in advance

ERROR

Traceback (most recent call last):
File "python", line 45, in
File "python", line 28, in get_average
TypeError: string indices must be integers, not str

Here's the complete script

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

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
score = homework + quizzes + tests
return score
'''
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
avg = Homework * .1 + quizzes * .3 + tests * .6
print 'weighted average = ', avg
return avg
'''

get_average("alice")


#8

I don't believe this! The problem was that I was actually trying to call my function! removed the function call from the end and it worked!!


#9

that part counts the average of those numbers


#10

It was my understanding that len() only counts the number of items in a list. In the glossary, they use examples with letters (as shown below):

my_string = 'abcdef'
len(my_string)
6

Does this mean that len() acts differently with numbers than it does with letters?

Thank you all for your continued help and patience, I would love to wrap my head around this.


#11

Oh man, I freaking get it now - I simply needed to see the forest through the trees.

Answer: Add up all the numbers (total), then divide by how many numbers there are (len). derp!

I was so concerned about the function of len() that I forgot to look at the whole line.


#12

it seems that it must be like this :slight_smile:

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

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