4. For the record

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.

```python

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 Likes

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
4 Likes

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]

{'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. "

1 Like

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
2 Likes

@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.

3 Likes

print student not students.

This,

print student

Not this,

print students

1 Like

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:

5 Likes

Thanks a lot. Helped me

1 Like

You’re welcome :grin:

1 Like

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?

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

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

This is how i did it.

3 Likes

wow, it works. Thanks a lot!

1 Like

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

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

It works for me!

1 Like

Thank you this really helped :+1:

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

This should be pretty straight forward. Whats the problem?

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