Part of the role - string indices must be integers


#1

Hello! I’m having trouble with the Part of the role exercise. Can you guys help me?

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

When calling the function get_class_average() with a list as a parameter, I get the following error: string indices must be integers. From what I’ve understood, this means that my average() function is not expecting a string as an argument. Problem is, I know not how to solve this. :frowning:

I want to be able to use the students list, mentioned in the exercise instructions, as a valid parameter to the get_average() function, called via the get_class_average() function.

Help me! Please!


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 = float(sum(numbers))
  total /= len(numbers)
  return total

def get_average(student):
  homework = average(student['homework']) * 0.1
  quizzes = average(student['quizzes']) * 0.3
  tests = average(student['tests']) * 0.6
  print(homework + quizzes + tests)
  return homework + quizzes + tests
  
def get_letter_grade(score):
  if(score >= 90):
    return 'A'
  elif(score <= 90 and score >= 80):
    return 'B'
  elif(score <= 80 and score >= 70):
    return 'C'
  elif(score <= 70 and score >= 60):
    return 'D'
  elif(score <= 60):
    return 'F'
  
def get_class_average(class_list):
  results = []
  for i in range(len(class_list)):
    results.append(get_average(class_list[i]))
  return average(results)

#edit: Included the function call I attempted.
get_class_average([‘alice’])


#2

please show me the function call you attempted, its not present in your code


#3

Sorry! Here it is:
get_class_average([‘alice’])

The codeacademy exercise attempts the same call.

Thanks =)


#4

'alice' shouldn’t be a string, it should be the dictionary:

get_average('alice') # error
get_average(alice) # correct

as get_class_average calls get_average with a string, you get an error. as you can see if you run the above code.


#5

Thanks for pointing it out!


#6