Part of the Whole


#1



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


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)/len(numbers)
    return total
    
students = [lloyd, alice, tyler]

def get_average(students):
   homework = average(students["homework"])
   quizzes  = average(students["quizzes"])
   tests    = average(students["tests"])
   grade1 = homework * 0.10
   grade2 = tests    * 0.60
   grade3 = quizzes  * 0.30 
   return grade1 + grade2 + grade3
   
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"

score = get_average(lloyd)
value = get_letter_grade(score)
print value

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

The error I get is
"Oops, try again. get_class_average([alice]) resulted in an error: 'NoneType' object is not iterable".What does this mean? and how do I correct it?


#2

append() will modify the list and return None, so by storing the result of append in a variable, you overwrite the result list with None

which also expains the error: NoneType (None) is not iterable (can't be loop over), sum() which you use in average() function tries to loop


#3

so, should I not make it a variable ? i mean the append() value?


#4

no, you shouldn't store the result of append action in a variable because append returns None, which will overwrite your list with None


#5

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

How about this? But this shows

"Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected"


#6

a function ends the moment a return keyword is reached

so your function ends in the first itertion/run of your loop, meaning it can't calculate the correct class average for a class of two or more students


#7

3 posts were split to a new topic: Part of whole


#8

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

@stetim94 I've removed the return now but now the error is

Oops, try again. get_class_average([alice]) should return a number


#10

Why would you remove the return? The program says you should return the average. You simply didn't place return correctly.


#11

so, should it be placed outside the function?


#12

@death4u567, clearly you have a different issue, make a new (linked) topic. Let me help @msnitiz in this topic


#14

return always has to be in a function, return needs to be outside the loop so your function can complete all its iterations rather then ending the function in the first iteration/run of the loop


#15

@stetim94 yes.I get it but how do I place " return"

such that it is in the function but not stop the iteration in just first loop?


#16

place return outside the loop


#17

oh yes! thanks @stetim94
this worked !

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

#18