For the record How add the index name?


#1


https://www.codecademy.com/courses/python-beginner-en-qzsCL/0/4?curriculum_id=4f89dab3d788890003000096


Actually the code is correct but I want to know something, when I run the code the results is like this:

Lloyd
(90.0, 97.0, 75.0, 92.0)
(88.0, 40.0, 94.0)
(75.0, 90.0)
Alice
[100.0, 92.0, 98.0, 100.0]
[82.0, 83.0, 91.0]
[89.0, 97.0]
Tyler
[0.0, 87.0, 75.0, 22.0]
[0.0, 75.0, 78.0]
[100.0, 100.0]
None


What I want to know is how to print the index too like this:

Name Lloyd
Homework (90.0, 97.0, 75.0, 92.0)
Quizzes (88.0, 40.0, 94.0)
Tests (75.0, 90.0)

I know that I can do something like this

print "Name : " + item["name"]

But if I have like 100 items in my dictionary that would be bad idea.


lloyd = {
    "name": "Lloyd",
    "homework": [],
    "quizzes": [],
    "tests": []
}
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]
}

lloyd['homework'] = 90.0, 97.0, 75.0, 92.0
lloyd['quizzes'] = 88.0, 40.0, 94.0
lloyd['tests'] = 75.0, 90.0

students = [lloyd,alice,tyler]

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


#2

Well you could in your array add Homework Quizzes and Tests in quotation marks on each line with the corresponding grades for each student. Probably not a genius solution but it would add it to the console.log. If you wanted it outside the array I can't just immediately think of anything on how to keep the arrays and add that to the beginning of the array....


#3

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