8, Part of the Whole


#1

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

Code

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
weight_sum = (homework * .1) + (quizzes * .3) + (tests * .6)
return weight_sum

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:
grades = get_average(student)
results.append(grades)
return results

print get_class_average(students)

Error Message

Traceback (most recent call last):
File "python", line 53, in
File "python", line 49, in get_class_average
File "python", line 28, in get_average
TypeError: list indices must be integers, not str

Highlighted code represents the lines in the error message.

Not sure why this error is coming up since the string is for the key and presumably the call should be for the associated value (e.g. an integer).

Nonetheless assistance would be appreciated.

Melissa


#2

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

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

students = [lloyd,alice,tyler]

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

THIS IS MY CODE


#3

one question is that you did this with right whitespace or not ?

ERRORS ALONG WITH SOLUTIONS:

ERROR(i) : [line 53]
solution : i guess its whitespace problem, because i tried it with whitespace by copying your code no errors were produced
ERROR(ii) : [line 49]
solution : i think whitespace error again along , and one more thing that is with this-

It should be returning not only results, but also the average of it
so like this:
[return average(results)]

ERROR(iii) : [line 28]
solution : This error was caused because of the previous error


#4

Thank you both for your contributions.

  1. The initial error was solved by add return average(results) - and I get why that is now (I was attributing averages to get_average() function so I left out returning the average of the results).

  2. I did have an issue with students not being defined. I previously tried to define the dictionary students = [lloyd, alice, tyler] but I put it after the wrong function. I appreciate the code example above.

After these two changes the code works.

Regards,

Melissa


#5

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