How to improve this code? MINOR ADJUSTMENTS!


#1

My code works perfectly fine, but it prints out more than I think it should.

Is there a better way to do this? I tried to do something along the lines of splicing it i.e
print students[0:3, "name"] but I'm not smart enough for that. Could someone help me out with a better looking 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 item in students:
    print lloyd["name"]
    print alice["name"]
    print tyler["name"]
    print lloyd["homework"]
    print alice["homework"]
    print tyler["homework"]
    print lloyd["quizzes"]
    print alice["quizzes"]
    print tyler["quizzes"]
    print lloyd["tests"]
    print alice["tests"]
    print tyler["tests"]

#2

item will hold each student in turn which we can see here:

for item in students:
    print item

so you could do item["name"] to print the name. this way, the loop should just contain 4 lines of code


#3

for item in students:
print item["name"]
print item["homework"]
print item["quizzes"]
print item["tests"]

Thanks, managed to fix it! :slight_smile:


#4