-1

The console replied that the global name 'average' is not defined. That complete message was:

get_class_average([alice]) resulted in an error: global name 'average' is not defined

I can't find the cause but I have to say too that I'm just starting to learn python.

Code below...

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 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_class_average(students):
    results = []
    for student in students:
        results.append(get_average(student))
    return average(results)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 2
    Error clearly says `average` is not defined. Not `get_average`... – OneCricketeer Oct 25 '16 at 16:22
  • You used the wrong tags, this is not a SyntaxError, but a NameError. – CodenameLambda Oct 25 '16 at 16:24
  • 1
    Note the line `# Add your function below!`... This looks like a homework assignment and the error is literally calling out that you haven't done the assignment yet. You need a `def average(values):`... – Emyr Oct 25 '16 at 16:29
  • Nope ... nowhere in your code do you supply the function average. It's not a Python built-in function, either. Your question title says "but it is"; please supply the evidence for your assertion ... better yet, supply the code. As posted, your code doesn't produce any output. – Prune Oct 25 '16 at 22:16

1 Answers1

1

There is no builtin average function in python, but you can use this one:

def average(iterable):
    li = list(iterable)  # now you can supply generator expressions and stuff like that too
    return float(sum(li)) / len(li)
CodenameLambda
  • 1,486
  • 11
  • 23