8. Part of the Whole, 'results' not defined


#1



Link to excercise


Hello Team,

I've been looking around this Q&A Forum trying to figure out why I'm receiving the error message below:

I've tried to refresh the page. Looked through the forum and everyone else seems to have something similar to my code. Does anyone hace a clue why I'm receiving this message?

Error Message

Traceback (most recent call last):
File "python", line 52, in
NameError: name 'results' is not defined


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

-- updated, unloading all the code to reference

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 = 0.1 * average(student["homework"])
    quizzes = 0.3 * average(student["quizzes"])
    tests = 0.6 * average(student["tests"])
    return homework + quizzes + tests
    
def get_letter_grade(score):
    if score >= 90.0:
        return "A"
    elif score >= 80.0:
        return "B"
    elif score >= 70.0:
        return "C"
    elif score >= 60.0:
        return "D"
    else:
        return "F"

#print get_letter_grade(average(lloyd))

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


#2

Did you read this part in the instructions?:

Finally, return the result of calling average() withresults

You didn't return the result yet. Don't forget to indent your return statement so it matches the same indentation level as your for loop. Also, you can remove:

It will throw an error.


#3

Thanks for prompt response. It was like that prior. It is still not working, unfortunately.


#5

Hi @intermediategamer, just an FYI, I had to restart the entire module, fast-forwarded and then tried "new changes" to it and it seemed to work. I'm not sure what the issue was. I wasn't having trouble understanding or reading instructions but luckily it is now working. Thanks for your help!


#6

Sure, no problem :thumbsup: Happy coding! :smile:


#7

I have this same issue. This is my 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]
}

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 * homework + 0.3 * quizzes + 0.6 * tests
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):
students = [lloyd, alice, tyler]
results = []
for student in students:
results.append(get_average(student))
return average(results)
but i keep getting this error Oops, try again. get_class_average([alice]) returned 80.55 instead of 91.15 as expected`


#8

Greetings @theraregem, I'll be happy to help:

Let's start by removing students = [lloyd, alice, tyler] from your get_class_average function and place it right after your last dictionary tyler. You will call "students" afterwards but you may not have it within your function.

Let me know if this helps any.


#9

Thanks @nahuatl19650 i have done exactly what you said. However i am still getting the same error.
Here is the new 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]
}
students = [lloyd, alice, tyler]

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 * homework + 0.3 * quizzes + 0.6 * tests

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


#10

Can you paste the correct code that work for you here. Thanks


#11
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]

Add your function below!

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

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

return homework + quizzes + tests

def get_letter_grade(score):
if score >= 90.0:
return "A"
elif score >= 80.0:
return "B"
elif score >= 70.0:
return "C"
elif score >= 60.0:
return "D"
else:
return "F"

print get_letter_grade(get_average(lloyd))

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

print get_class_average(students)
print get_letter_grade(get_class_average)


#12

Thanks @nahuatl19650


#13

Problem Solved!
The instruction "forgot" to ask you to make a list with the students, therefore by adding the "class_list" and defining as the argument of the new function the problem is gone!

class_list = [lloyd, alice, tyler]

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


#14

This will work
copy it

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 = 0.1 * average(student["homework"])
    quizzes = 0.3 * average(student["quizzes"])
    tests = 0.6 * average(student["tests"])
    return homework + quizzes + tests
    
def get_letter_grade(score):
    if score >= 90.0:
        return "A"
    elif score >= 80.0:
        return "B"
    elif score >= 70.0:
        return "C"
    elif score >= 60.0:
        return "D"
    else:
        return "F"

#print get_letter_grade(average(lloyd))
students = [lloyd, alice, tyler]

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

my result


#16

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