1.4. For the Record integers, not str


#1



For the Record Link

Hi there!
I have the following console message :
Traceback (most recent call last):
** File "python", line 23, in **
TypeError: tuple indices must be integers, not str

I can't find the error. It would be awesome if someone could point it out to me!

Here is my code:


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]

for student in students:
    print student["name"]
    print student["homework"]
    print student["quizzes"]
    print student["tests"]


#2

This is most likely wrong, but maybe check the commas.

lloyd, alice, tyler
lloyd, alice ,tyler


#3

thanks but that has not changed anything


#4

You have commas after your end curly brackets


#5

The problem is in your for loop, the value in the variable student is a type string and not a the dictionary type.


#6

in loop

print student["name"]

actually need to be something like

print student[0]["name"]

so u need to get their index, to avoid str or dict value error

print students[students.index(student)]["name"]

this is my solution, but i think that can be done in a more simple way

**for student in students:
print student["name"]
print student["homework"]
print student["quizzes"]
print student['tests']**

easiest way


#7

Thanks @wenomr and @netjumper98552 - your for loop got me going the right direction.


#8