4. For the Record


#1




The error message comes "Oops, try again. Your code looks a bit off--it threw a "list indices must be integers, not dict" error. Feel free to peek back at earlier exercises if you need a syntax refresher! "


I expect it to display the scores of the students.


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
    print "homework: %s" % students[student]
    print "quizzes: %s" % students[student]
    print "tests: %" % students[student]


#2

here:

print "homework: %s" % students[student]

what is students? its a list, contains multiply students. What you are trying to do is access the dictionary, but students is no dictionary, which variable is a dictionary?


#3

I reformatted my code like this:

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

... and it gave the message "Oops, try again. Your code looks a bit off--it threw a "slice indices must be integers or None or have an index method" error. Feel free to peek back at earlier exercises if you need a syntax refresher! "

I know I basically have to call a student from the list "students" and list his/her homework, quizzes and tests scores, which are stored as dictionaries.

I haven't specified the dictionary name because they all contain the same items.


#5

but look at this code:

for student in students:
    print student

what is student? as you can see, its a dictionary. The for loop loops over your list which contains the dictionaries.

so you can use student["keyname"] to retrieve values


#7

It worked!

But another error came up: "Oops, try again. Did you print out each student's name? It looks like "Lloyd" wasn't printed. "

I started inspecting the output but "Lloyd" was there.


#8

can i see an updated version of your code?


#9

3 posts were split to a new topic: 4. for the record


#10

here it is...

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
    print "homework: %s" % student["homework"]
    print "quizzes: %s" % student["quizzes"]
    print "tests: %s" % student["tests"]

#11

here:

print student

this will print the dictionary, you should print the value of the name key of the dictionary


#13

3 posts were split to a new topic: [solved]for the record


#16

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