For the Record


#1

Hi! Can anybody help me about these codes? I don't know where I did wrong.
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]
print students[name]
print students[homework]
print students[quizzes]
print students[tests]


#2

The above are all undefined. The students list consists of references to dictionaries to which the keys apply.

print students[lloyd][name]     # Lloyd

The presence of an iterable gives rise to an iterator, as in a loop construct that can inspect each dictionary referenced in the list.

for student in students:

comes to mind. It will iterate the list, and inspect each dictionary, in turn.

    for key in student:
        print (key,student[key])
    print ("=" * 32)

The output is,

name Lloyd
quizzes [88.0, 40.0, 94.0]
tests [75.0, 90.0]
homework [90.0, 97.0, 75.0, 92.0]
================================
name Alice
quizzes [82.0, 83.0, 91.0]
tests [89.0, 97.0]
homework [100.0, 92.0, 98.0, 100.0]
================================
name Tyler
quizzes [0.0, 75.0, 78.0]
tests [100.0, 100.0]
homework [0.0, 87.0, 75.0, 22.0]
================================

Perhaps not what the lesson expects, but we must be on the right track. Study this example and learn from it. There is lots to be had.


#3

I'm still quite new here, but i managed to pass the lesson with the following input (thanks for the above post for helping me get to it)

students=[lloyd,alice,tyler]

for student in students:
for key in student:
print(student[key])

prints out:
[88.0, 40.0, 94.0]
[75.0, 90.0]
Lloyd
[90.0, 97.0, 75.0, 92.0]
[82.0, 83.0, 91.0]
[89.0, 97.0]
Alice
[100.0, 92.0, 98.0, 100.0]
[0.0, 75.0, 78.0]
[100.0, 100.0]
Tyler
[0.0, 87.0, 75.0, 22.0]

which is what it seems to have wanted... I guess it wanted only the data and no other items. As in it didn't want the key attached to it as you have above.

hope this helps you!


#4

Demo code is not meant to be able to pass, though at times it might. It's a learning model.