Part of the Whole trouble with Alice


#1

I have finally come here to ask, I continue to get an error when running my for loop for the Alice key.

When I try to run this, I keep receiving this error:
get_class_average([alice]) resulted in an error: global name 'average' is not defined>

I think the get_average function is correct, and I am not sure what the error means. Any thoughts?


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!
students = [lloyd, alice, tyler]
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return homework * .1 + quizzes * .3 + tests * .6

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(student)
    return average(results)


#2

Your code is missing the first function, this one

def average(numbers):
    total = sum(numbers)
    total = float(total)
    total = total / len(numbers)
    return total

Your code is fine you just probably deleted that piece of code while writing the rest.


#3

I take that back, since I can't delete this I will follow up: I had deleted the Average function. When I reinserted it above I now receive this error: "Oops, try again. One of the following is missing or broken when we tried to use it: alice, lloyd, tyler, students, get_class_average, get_letter_grade"


#4

Post your code with the added function please


#5

Thank you! I added it below the list, all the indentation should be a ok.

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]
}

students = [lloyd, alice, tyler]

import math
def average(numbers):
total = float(sum(numbers))
return total / len(numbers)

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
return homework * .1 + quizzes * .3 + tests * .6

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(student)
return average(results)


#6

Awesome! If that was the answer you were looking for feel free to click the solution button below my reply. :grin:


#7

Hi
Can anybody help me with this recurring error. Below is the code
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]
}
import math
def average(numbers):
total = float(sum(numbers))
avg = total/len(numbers)
return avg
def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
sum = 0.10*average(student["homework"]) + 0.30*average(student["quizzes"]) + 0.30*average(student["tests"])
print sum

I am continuously getting error "Oops, try again. get_average(alice) returned None instead of the expected 91.15"

Help!


#8

You don't need this line.


#9

Here, you shouldn't store these values in a variable just return all of them as if part of the function.


#10

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


#11

Hi
tried the same. No improvement. Still getting the same message error "Oops, try again. get_average(alice) returned None instead of the expected 91.15"


#12

Post your code with those two errors fixed, so we can find the other errors.


#13

Here is the code

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]
}
def average(numbers):
total = float(sum(numbers))
avg = total/len(numbers)
return avg
def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
return 0.10 * average(student["homework"]) + 0.30 * average(student["quizzes"]) + 0.30 * average(student["tests"])


#14
def average(numbers):
    total = float(sum(numbers))
    avg = total/len(numbers)
    return avg

Your average function is also abit incorrect.

You can even in store everything in the same variable like this,

def average(numbers):
    total = sum(numbers)
    total = float(total)
    total = total / len(numbers)

#15

That's the final error because after correcting your get_average andaverage function your code should work.


#16

Sorry but still there is a problem. Now the error reads like
Oops, try again. get_average(alice) raised the following error: unsupported operand type(s) for *: 'float' and 'NoneType'


#17

Post your code with all errors corrected. By now your code should work. :smile:


#18

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)
s=total/len(numbers)
return s
def get_average(student):
homework=average(student["homework"])
quizzes=average(student["quizzes"])
tests=average(student["tests"])
a=0.1*homework+0.3*quizzes+.6*tests
return a
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"
get_letter_grade(get_average(lloyd))
def get_class_average(students):
results=[]
for student in students:
b=get_average(student)
results.append(b)
return average(results)


#19

@biscostu54 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)
s=total/len(numbers)
return s
def get_average(student):
homework=average(student["homework"])
quizzes=average(student["quizzes"])
tests=average(student["tests"])
a=0.1*homework+0.3*quizzes+.6*tests
return a
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"
get_letter_grade(get_average(lloyd))
def get_class_average(students):
results=[]
for student in students:
b=get_average(student)
results.append(b)
return average(results)


#20

this is happening to me too

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]
}

students = [lloyd, alice, tyler]

def average(numbers):
    total = sum(numbers)
    total = float(total)
    total = total / len(numbers)
    return total
    print total

def get_average(student):
    homework = average(student["homework"]) * .1
    quizzes = average(student["quizzes"]) * .3
    tests = average(student["tests"]) * .6
    return sum([homework, quizzes, tests])
    print sum([homework, quizzes, tests])
    
def get_letter_grade(score):
    if type(score) == int or type(score) == float:
        if score >= 90:
            return "A"
            print "A"
        elif score >= 80:
            return "B"
            print "B"
        elif score >= 70:
            return "C"
            print "C"
        elif score >= 60:
            return "D"
            print "D"
        else:
            return "F"
            print "F"
print get_average(alice)

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

same error