0

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.

Alex
  • 18,484
  • 8
  • 60
  • 80
Fede Couti
  • 281
  • 2
  • 3
  • 13
  • Oh shoot, I marked it as the wrong duplicate. I thought you were looping over a dictionary at the bottom in `get_class_average()` – Dietrich Epp Mar 11 '15 at 04:34
  • Not a dup, please reopen. (May be too localized though?) – Platinum Azure Mar 11 '15 at 04:37
  • 2
    You could use, strategically placed, print statements to *see* what is happening. Print things like function arguments, function return values, and *variables*. You might want to start in the problem function. – wwii Mar 11 '15 at 04:39

2 Answers2

3

Your problem is this line of code:

students = ["john", "paul", "tyler"]

You are setting your students list to be a list of strings, not a list of dictionaries. Remove the quotations.

students = [john, paul, tyler]

Now you have a list of your student dictionaries and the program runs with the following output:

83.8666666667
B
Andy
  • 49,085
  • 60
  • 166
  • 233
0

Your students array is an array of strings (the literal strings "john", "paul", "tyler").

You should create the array using the variable names (no quotes).

students = [john, paul, tyler]
Platinum Azure
  • 45,269
  • 12
  • 110
  • 134