#4 For The Record


I need help with this exercise basically it asks me to print out students data as in format : name, homework, quizzes and tests.

For this do I have use the following code : print students ["key"] 4 times ?

Can you also tell me why wouldnt this code work to do exact same

for key,val in students:
print key + value



Try it out for yourself and see! Never let Codecademy's rules keep you from experimenting.

for student in students:
    for key in student:
        print key, student[key]

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

When you step through a dictionary using for key in dict: what you get back is not in any particular order. I can't find any way to order them. If you want names first, you'll have to do it manually as the exercise suggests.


So long as it is on our own, and not in the graded lesson. Pass the exercise by following the instructions. The SCT is not very flexible.


The only sure way is to use an index, which maintains its order.

keys = ['name', 'homework', 'quizzes', 'tests']

for student in students:
    for i in range(len(keys)):
        print keys[i], student[keys[i]]

However, the lesson is not the place to explore this since it is not in the instructions or the lesson text.



I know this might sound little stupid...but could you explain this code. specially combination of range and len function..

keys = ['name', 'homework', 'quizzes', 'tests']

for student in students:
for i in range(len(keys)):
print keys[i], student[keys[i]]


Recall that the range function takes at least one argument, an integer.

Recall also that the len function returns an integer, describing the length, or number of elements of the argument that it takes.

n = len(keys)
for i in range(n):

Is a more explicit representation of the original code. They give the same result. When we pass a function call as the argument in another function call, the argument call is made first and the return value from that function becomes the argument of the outer call.


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