I have to make a simple Python program that allows to get the average of three different students and eventually get the class average and print it on the screen. The code is the following:
john = {
"name": "John",
"homework": [90.0, 97.0, 75.0, 92.0],
"extras": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
paul = {
"name": "Paul",
"homework": [100.0, 92.0, 98.0, 100.0],
"extras": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"extras": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
students = ["john", "paul", "tyler"]
def average(numbers):
total = sum(numbers)
total = float(total)
return total/len(numbers)
def get_average(student):
homework = average(student["homework"])
extras = average(student["extras"])
tests = average(student["tests"])
return homework*0.1 + extras*0.3 + tests*0.6
def qualifications_in_letters(result):
if result >= 90:
return "A"
elif 80 <= result < 90:
return "B"
elif 70 <= result < 80:
return "C"
elif 60 <= result < 70:
return "D"
else:
return "F"
def get_class_average(students):
marks = []
for student in students:
marks.append(get_average(student))
return average(marks)
print get_class_average(students)
print qualifications_in_letters(get_class_average(students))
Each student has a dictionary with four keys and values (name, homework, extras and tests). What the program should do is to take the marks of the students, get their individual average and then get the class' average. The homework represents the 10% of each student's average; the extras the 30% and the tests the 60%.
There is a list that has the names of the students as elements. The problem is that when I run the code this error message appears:
Traceback (most recent call last):
File "<stdin>", line 52, in <module>
File "<stdin>", line 49, in get_class_average
File "<stdin>", line 28, in get_average
TypeError: string indices must be integers, not str
I don't know how to fix it.
Line 28 is the second one in get_average()
; line 49 the one inside the for
loop and 52 is the pne where get_class_average()
is called.