Why isn't this code working?


#1

https://www.codecademy.com/courses/learn-python/lessons/student-becomes-the-teacher/exercises/part-of-the-whole?action=lesson_resume

I have tried everything, but the error message is still the same, i.e my code returns 91.15 as the result rather than 85.85 for get_class_average(“alice”,“lloyd”). I really can’t figure out where the problem is, please help me.

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 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 average(numbers):
  total=0
  total= float(sum(numbers))/len(numbers)
  return total
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_class_average(class_list):
   results=[]
   for student in class_list:
       results.append(get_average(student))
       return average(results)

#2

a function ends the moment a return keyword is reached, so your loop ends in the first iteration of the loop, to get the correct class average, the loop needs to make all its iterations, not finish in the first iteration


#3

Could you please elaborate a little on that, I tried calling average(results) once without the return function and then with it again, but the same error message is showing.


#4

you want to place return outside the loop, so the loop can make all its iterations


#5

Ah yes, finally! It works. Thankyou so much! :slight_smile:


#6