Part of a whole


#1


https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/4?curriculum_id=4f89dab3d788890003000096

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"
    else:
        return "F"
        
get_letter_grade(lloyd)
def get_class_average(students):
    result = []
    for x in students:
        result = get_average(student)
        results.append(result)
       
        return average(results)


#2

You don't need this line,

And here,

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

results.append(get_average(student))

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


#3

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


#4

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


#5

Let me see your updated code


#6

Can I please see your code


#7

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


#8

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:
        result.append(get_average(student))
    return average(result)

#9

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.


#10

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:
        results.append(get_average(student))

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

    return average(results)

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

#11

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


#12

i tried the same but still not working.


#13

post your code here, I check it then


#14

this is what i needed...


#15

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