5. It's Ok to be average


#1





Can smo help me?

lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}

Surprisingly, I did it)) I just get back to the previous lesson and run it, after I run the 5th lesson with the same code and it worked!

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

Error:

Oops, try again. average([3, 0]) resulted in an error: global name 'total' is not defined


I found the code in other's aid request. Please, tell me what's wrong and what the argument "numbers" is doing here?

Sorry for my bad English. Thank you!


#2

Surprisingly, I did it. I get back to the previous lesson and run it. After, I just run the same code and it worked.


#3

thank you for posting this it acctually really helped :smile:


#4

OK -- I don't get it. We do not define "numbers" in this screen -- how does the function know what to do? I thought that maybe in later lessons we would use it, but that also does not appear to be the case. Why did we do this or what is this accomplishing? Thanks!


#5

two things to understand first:
1. function only execute when called
2. numbers is the function parameter.

function parameters serve as placeholder until you call the function, in which case you supply an argument to satisfy the parameter. Given the function won't execute until called, the parameter can be seen as a placeholder


#8

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