Your code looks a bit off--it threw a "list indices must be integers, not str" error


#1

Your code looks a bit off--it threw a "list indices must be integers, not str" error. Feel free to peek back at earlier exercises if you need a syntax refresher!
Above error is thrown.

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

If you not able to look there, adding my code below.

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 students["name"]
print students["homework"]
print students["quizzes"]
print students["tests"]

Let me know what i am doing wrong. thanks :slight_smile:


#2

When you loop through students list using iteration variable student ,the students contains dictionaries ( lloyd, alice, tyler ) and thus you have to access different data from these dictionaries,(student)

so student (iteration variable) is a dictionary
students is a list that contains all these dictionaries


#3

If i try to print this way

for student in students:
print students[0]
print students[1]
print students[2]

Still i get error saying -- Oops, try again. Did you print out each student's name? It looks like "Lloyd" wasn't printed.

Can u please tell me exactly what should be the command ?


#4

That will print the list items, but you have to print different values of your dictionaries using key ? your dictionary is student (iteration variable)

one hint can be...

student["name"] will give me name of corresponding dictionary .
You have to do same thing to print values corresponding to "homework" , "quizzes" and "tests" .


#5

Thanks. It worked :slight_smile:


#6

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