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


#1

This returns this error:
get_class_average([alice]) resulted in an error: global name 'get' 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)
    average = total / len(numbers)
    return average
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    weighted_average = homework * 0.1 + quizzes * 0.3 + \
    tests * 0.6
    return weighted_average

def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"
    print get_letter_grade(get_average(lloyd))

def get_class_average(students):
    results = []
    for student in students:
        get_average(student)
        results.append(get_average(student))
    return results.append(get-average(student))

#2

you should just return results, which is a list with the results.

the for loop appends the items to the list, then after the loop you return the list


#3

I tried it but it returned this error:

get_class_average([alice]) should return a number

This is how my code looks now:

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)
    average = total / len(numbers)
    return average
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    weighted_average = homework * 0.1 + quizzes * 0.3 + \
    tests * 0.6
    return weighted_average

def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"
    print get_letter_grade(get_average(lloyd))

def get_class_average(students):
    results = []
    for student in students:
        get_average(student)
        results.append(get_average(student))
        return results

#4

why did you change the indent of return? Now a return keyword is reached in the first run of the loop, causing the function to end.

make sure return is after the loop


#5

I've fixed the indentation but the same error comes up.


#6

result is a list, how silly of me. you need to return the average:

return average(results)

see step 4 of the instructions:

Finally, return the result of calling average() with results.


#7

It fixed the problem. Thanks! But I sort of feel lost in this section. What does the whole code in this section aim to achieve?


#8

to build a bunch a function to preform tasks on you dictionary's, like calculating averages, class averages and so on


#9

So which of the functions in my code represents the class average?


#10

this one:

def get_class_average(students):

the one you just build. it can take one or more students as input (in the form of a list), gets the averages:

results.append(get_average(student))

this will get the weighted average of each student (see get_average function)

then return the average of all the students:

return average(results)

#11

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