4. For the record


#1

Hello. I was wondering if there is a smarter way to do this exercise instead of having to type all of the keys for the students. I am thinking maybe an other loop that will loop through all of the keys in the students' dictionaries and print out the value corresponding to that key. like a double loop.


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

The reason for your for loop was to avoid what you've just done you only need to print students like this

for students in students:
     print students

#3

Just wondering why I am not able to run this 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 key in students:
print key[0]
print key[1]
print key[2]
print key[3]


#4

{'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]}
None

I've tried this before, but it prints out an error

"Oops, try again. Did you print out each student's name? It looks like "Lloyd" wasn't printed. "


#6

You are trying to access a dictionary as if it is a list that cannot work.

To print all your data just create a for loop and print your stored dictionary that is in the list students.

for student in students:
    print student

#7

@arrayjumper81430

You can use nested loops:

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

But you have no control on order of printing.


#8

print student not students.

This,

print student

Not this,

print students


#9

Hi!

You have the students part right, but the word "key" does not belong. I tried for student in students and it worked.

Second, when printing you need to "access" the item stored in the key so you will essentially be writing:

print student[insert key here]
print student[insert key here]
print student[insert key here]
print student[insert key here]

Remember, your keys are what contains the grades :wink:


#10

Thanks a lot. Helped me


#11

You're welcome :grin:


#12

hi dave, i just copied your code directly but it still shows
"Oops, try again. Did you print out each student's name? It looks like "Lloyd" wasn't printed."

does it work on your console?


#13

You can try using nested loops but the order will be scrambled,

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

#15


This is how i did it.


#16

wow, it works. Thanks a lot!


#17

I tried the exact same method and it didn't work out.


#18

def students(student):
for student in students:
print student[name]
print student[homework]
print student[quizzes]
print student[tests]

It works for me!


#19

Thank you this really helped :+1:


#20

You could do it this way
for student in students:
print student["name"], student["homework"], student["quizzes"], student["tests"]


#21

This should be pretty straight forward. Whats the problem?


#22

the variable students is not a dictionary so you can't use it this way.
print students ['name']

the variable students you have used now is actually a list
so if you want to print out all the names in the list, you can do this:
for x in students:
print x