No. 9 - How is everybody doing?


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/student-becomes-the-teacher/exercises/how-is-everybody-doing-?action=lesson_resume
<In what way does your code behave incorrectly? Include ALL error messages.>
This is what went wrong:
Traceback (most recent call last):
File “python”, line 51, in
File “python”, line 48, in get_class_average
File “python”, line 28, in get_average
TypeError: string indices must be integers, not str<What do you expect to happen instead?>


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]
}


def average(numbers):
  total = sum(numbers)
  total = float(total)
  total = total / 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_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"

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

print (get_class_average(students))
print (get_letter_grade(score))

#2

here:

print (get_class_average(students))
print (get_letter_grade(score))

students and score are undefined. You build the students list in 3. put it together, but the list is now missing

you should supply the class average as argument to calling get_letter_grade, why would you use score?


#3

I’m not sure why I put score in there, thanks for pointing that out. It still says that string indices must be integers, not str. I could have easily missed this out, but where did it say how score should be defined?Thanks for any help. This is what i’ve put now instead:

print (get_class_average(students))
print (get_class_grade(get_class_average))

should i put average instead of get_class_average?


#4

here:

print (get_class_average(students))

did you add the students list again? Otherwise students is undefined

here:

print (get_class_grade(get_class_average))

get_class_grade is undefined, its get_letter_grade. The only purpose of get_letter_grade is to convert number grade to letter grade, so first you need to calculate the class average (83.something if i remember correctly) then you need to convert this number grade to letter grade

If you need more help, post your full code again


#5

Thanks for your help, unfortunately i still need more:

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]
}

students = ["lloyd", "alice", "tyler"]

def average(numbers):
  total = sum(numbers)
  total = float(total)
  total = total / len(numbers)
  return total

def get_average(student):
  homework = average(student["homework"])     (line 29)
  quizzes = average(student["quizzes"])
  tests = average(student["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"

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

print (get_class_average(students))           (line 52)
print (get_letter_grade(get_class_average))

This is the problem it comes up with:
Traceback (most recent call last):
File “python”, line 52, in
File “python”, line 49, in get_class_average
File “python”, line 29, in get_average
TypeError: string indices must be integers, not str

I have labelled the lines which it highlights and says are wrong, thank you for your help. Sorry if i’m doing anything stupid.


#6

your students list:

students = ["lloyd", "alice", "tyler"]

contains strings, why? You need to store the dictionaries in the list. calling get_average with a string argument will result in an error

here:

print (get_letter_grade(get_class_average))

get_class_average() should be a function call, to calculate the class average


#7

so should it be (get_letter_grade(get_class_average(students)? When i run this it like that it works without stating an error for this statement. Also, when i change the code to the code below, there is only an error on line 52:

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

print (get_class_average(student)) (line 52)
print (get_letter_grade(get_class_average))

It ends up saying student is not defined so when i change it to students, line 49 suddenly comes up with an error stating:

Traceback (most recent call last):
File “python”, line 52, in
File “python”, line 49, in get_class_average
TypeError: ‘function’ object has no attribute ‘getitem

Thanks for your help.


#8

what are you doing? For every step i try to push you in the right direction, you take two steps back

here:

print (get_class_average(student)) (line 52)

why did you change it to student?

why did you change the loop in your get_class_average function?

here:

results.append(get_average[students]) 

get_average() is still a function call, why did you switch to square brackets all of a sudden?

What is it you need from me? How can i help you in a way that we are going in the right direction?

I mean, given you already build your students list earlier, i thought just linking to exercise would be enough, given you already completed the exercise adding the list correctly should have been easy

What is it, that you struggle to understand?

The code you posted when creating the topic is closer to the solution then we currently are

I don’t understand why you messed with everything, all you needed to do was:

adding students list:

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

# add your students list here


print (get_class_average(students))
print (get_letter_grade(score))

and fix print (get_letter_grade(score)) function call, that is it. Yet here we are, further away from the solution


#9

I’ve done all this because I’m confused so i trial and error things to see if they work, which i will now stop doing. Also I want nothing from you now apart from to accept my apologies for me being dumb. I changed the [] to () with the results.append(get_average(students) and then it worked completely fine. Thank you so much for your help, sorry for aggravating you though.


#10

Trail and error is a fine way to learn a bit about how certain things work, but for the helping side if someone goes on trail and error attempt, its really difficult to help

It isn’t dumb, but a trail and error attempt has great learning value when done on your own

Good you got it solved now :slight_smile:


#11