For the record quick question


#1

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"]

Is there faster way to do this other name printing out every single command like print student["name"] (homework, quizzes, test)? my originial assumption was to do ( print student["name", "homework", "quizzes", "tests] but it didnt work


#3

There are faster ways of outputting all of the data, however, they are not necessarily better. Codecademy's submission correctness test (SCT) might not accept some of the following solutions, but you can experiment with them for practice.

This is the shortest, however, the result is cluttered, and the SCT will not accept it ...

print students

The following will display all the data, and the SCT will accept it, however, in my opinion, it is unsatisfactory, since it might not display the student's name first, because Python dict objects are unordered. Therefore, the order of the output is confusing to the reader ...

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

The following is longer, and labels all the data, however Codecademy's SCT for this exercise will not accept it ...

for student in students:
    print "\nGrades for {:s}".format(student["name"])
    print "Homework: {:s}".format(" ".join([str(grade) for grade in student["homework"]]))
    print "Quizzes: {:s}".format(" ".join([str(grade) for grade in student["quizzes"]]))
    print "Tests: {:s}".format(" ".join([str(grade) for grade in student["tests"]]))

Output ...

Grades for Lloyd
Homework: 90.0 97.0 75.0 92.0
Quizzes: 88.0 40.0 94.0
Tests: 75.0 90.0

Grades for Alice
Homework: 100.0 92.0 98.0 100.0
Quizzes: 82.0 83.0 91.0
Tests: 89.0 97.0

Grades for Tyler
Homework: 0.0 87.0 75.0 22.0
Quizzes: 0.0 75.0 78.0
Tests: 100.0 100.0

#4

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