Need help in lesson 9/9


#1

exercise url: https://www.codecademy.com/courses/learn-python/lessons/student-becomes-the-teacher/exercises/how-is-everybody-doing-?action=lesson_resume
My code is :

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)
  return float(total)/len(numbers)
def get_average(lloyd):
  homework = average (lloyd["homework"])
  quizzes = average(lloyd["quizzes"])
  tests = average(lloyd["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" 

print get_letter_grade(get_average(lloyd))
def get_class_average(class_list):
  results = []
  for student in class_list:
    student_average = get_average(student)
    results.append(student_average)
  return average(results)
    
students = ("lloyd","alice","tylor")
print get_class_average(students)
print get_letter_grade(class_average)

the error message on console is : B
Traceback (most recent call last):
File “python”, line 50, in
File “python”, line 45, in get_class_average
File “python”, line 25, in get_average
TypeError: string indices must be integers, not str


#2

here:

students = ("lloyd","alice","tylor")

why are you students strings, i mean here:

print get_letter_grade(get_average(lloyd))

you use the dictionary stored in a variable lloyd, so you realize that the students should refer to the dictionary, i am unsure why you use strings in the list

also, get_average(lloyd), the function has a parameter of lloyd, why? it could be any student


#3

Thanks i removed the quotation
But I did not get your point about get_average(lloyd)


#4

ok you are saying it could be students instead of lloyd right?


#5

but do you also understand what implications this has on your program?

well, student would be even a better name, given get_average takes a single student as argument. but then these lines:

  homework = average (lloyd["homework"])
  quizzes = average(lloyd["quizzes"])
  tests = average(lloyd["tests"])

also need updating


#6

Yes I do understand what implications it has for my program
letters inside of quotation are strings and I was calling string lloyd instead of dictionary lloyd


#7

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