6. Just Weight and See. Where is `student` defined?


#1



6. Just Weight and See

I do not understand where student is defined in this code.

After I got this working, I added print student["homework"] to line 22 just to see what would print. I receive an error. I am confused because student is not defined anywhere, but the code seems to work fine if I remove line 22.


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!

print student["homework"]

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"])
    return homework * .1 + quizzes * .3 + tests * .6


#2

student exist inside your get_average because it is the function parameter, but it doesn't exist outside the function.

Code runs in logical order (from top to bottom), by the time it comes to line 22, there is no variable student


#3

I guess I don't quite understand how the get_average function knows to look at the student list to find "homework" "quizzes" and "tests" when the student parameter is not defined anywhere else.


#4

Oh, I think I just answered my own question! The function does not yet know how to cycle through the students. It's just a function, and the parameter is not yet defined as anything...


#5

You have to tell the get_average function where to look.

get_average(lloyd) will look in the lloyd dictionary, while get_average(tyler) will look in the tyler dictionary.


#6

take a look at this simple example:

print student
def example_function(student):
   print student
example_function("meganinja")
print student

which will give you an error, since student doesn't exist. Comment out the first line:

#print student
def example_function(student):
   print student
example_function("meganinja")
print student

now, the function is called, and will print meganinja, but then after function is another print statement, giving an error, so what is happening? When you create the function (example_function(student)) you pass a parameter (student), which is now a local variable inside the function (it only exist inside the function), then when you call the function (functions are just code blocks, they only execute when called), so we call the function (example_function("meganinja")), we pass the argument "meganinja" into the function, so the parameter student will now hold the argument "meganinja"`, but only inside the function, same with your get_average, to get a even better understanding, look at this code:

def example_function(student):
   print student
example_function("meganinja")
example_function("stetim94")