Part of a whole


My code is throwing a syntax error and says that alice is not defined in a global error.

Replace this line with your 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) 
    total = total/len(numbers)    
    return total

def get_average(student):
    homework = average(student["homework"])  * .10
    quizzes = average(student["quizzes"])  * .30 
    tests = average(student["tests"])  * .60
    return homework+quizzes+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"
        return "F"
def get_class_average(students):
    result = []
    for x in students:
        result = get_average(student)
        return average(results)


You don't need this line,

And here,

you need to include the get_average function so it would look something like this,


And your return statement should be on the same indentation level as your for loop. :slight_smile:


O.K. after implementing your suggestions in "Part of a whole" my code is throwing the following:
syntax error: 'return' outside function


This code somehow doesn't work for me. It returns an error message stating that the name "students is not defined". Somebody, please help.


Let me see your updated code


Can I please see your code


Finally solved it. It may have been an indentation error. Thanks!!!!


Can someone tell me why adding in " students = [lloyd, alice, tyler] " breaks this section? it actually changes the math equation for some reason and throws an error stating get_class_average([alice]) returned 83.866667 instead of the expected 91.15.

def get_class_average(students):
    result = []
    students = [lloyd, alice, tyler]
    for student in students:
    return average(result)


in the for loop on this line

result = get_average(student)

it should be x instead of student. Since student isn't defined in the for loop you have to use x. now if the for loop above was written as

for student in students

it would have worked.


okay lets go step by step:

Define a function called get_class_average that has one argument students. You can expect students to be a list containing your three students.

def get_class_average(students):

First, make an empty list called results.

results = []

For each student item in the class list, calculate get_average(student) and then call results.append() with that result.

    for student in students:

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

    return average(results)

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


Thank you very much! The last line helped me a lot: I forgot to write "average()" calling!=)


i tried the same but still not working.


post your code here, I check it then


this is what i needed...


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