8. Part Of The Whole


#1

I'm completely stuck and need help, I cant see any problems and I keep getting this error
"Oops, try again. get_class_average([alice]) resulted in an error: local variable 'average' referenced before assignment "

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 = [tyler, lloyd, alice]

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

#2

Your use of the average variable in the get_average (2nd) function conflicts with your use of calling the average() function in your get_class_average (last) function.

Try not to have the same name for a variable AND a function. Confusion arises.


#3

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)
return total / len(numbers)
def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
return 0.1 * average(student["homework"]) + \
0.3*average(student["quizzes"]) + 0.6*average(student["tests"])
def get_letter_grade(number):
if number >=90:
return "A"
elif number >=80:
return "B"
elif number >=70:
return "C"
elif number >= 60:
return "D"
else:
return "F"
return get_average(lloyd)
def get_class_average(students):
for item in students:
for student in students:
avg=get_average(student)
results.append(avg)
return average(results)
I am facing the same!!


#4

Why do you have 2 return statements right after each other?

Why do you have 2 of the same loop?


#5

It says ..Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected.. please help me out!! where is the fault?

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

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
return homework * 0.1 + quizzes * 0.3 + tests * 0.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))

students = [lloyd, alice, tyler]

def get_class_average(students):

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

#6

delete the whitespace behind your return, make it in line with the "for", that indentation returns the value to the wrong place


#7

Thanks a lot! :smiley:


#8

I had the same problem with the white space behind return, but can you explain to me why this is the case?


#9

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