What is Codecademy checking for to be printed?

Question

What is Codecademy checking for to be printed?

Answer

Like all exercises on Codecademy that ask you to name a variable, assign a value, or print something, the name, value, or thing being printed must match what’s asked for exactly.
For example, if the exercise wants us to print the student’s name, and we added something like this: print "Student’s name is: " + student["name"], that would definitely look great!
However! The Codecademy tests running behind the scenes need you to output exactly what’s being asked for, so just the name by itself. Same with the other values.
After this lesson, however, you can definitely customize it as much as you want without worrying about passing or not, or you can take your code to an in-browser Python environment, like the one found at repl.it - just type in Python and be sure not to use Python 3.

4 Likes

3 posts were split to a new topic: How does Python access the items in the list?

What’s going on here? I’ve puzzled as to why this isn’t working:

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 students in students:
    print students["name"]
    print students["homework"]
    print students["quizzes"]
    print students["tests"]
    print 

This is my output:

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]

Do you mean,

for student in students:
    print (student['name'])
    # ...
1 Like

I made this (a for inside a for):

-----for student in students:
----------for data in student:
---------------print student[data]

and my outputs were “disorganized”, for example:

[0.0, 75.0, 78.0]
[100.0, 100.0]
Tyler
[0.0, 87.0, 75.0, 22.0]

I want to know why does this print the data in this order, and not the order I see in the dictionary

1 Like

Dictionaries have only been ordered (insertion order) since Python version 3.6 and on. Before then they were unordered structures. Since you’re using old style print I guess this is python2 in which case the key-value pairs are not based on insertion order.

So the output of looping through them won’t necessarily be the order the items were added to the dictionary. Instead you may have to access the keys with an ordered structure such as a List if you want a more exact output, e.g.

students = [lloyd, alice, tyler]
# new list to loop through keys in a specific order-
ordered_key_names = ['name', 'homework', 'quizzes', 'tests']
for student in students:
    for key in ordered_key_names:
        # as an example...

There is an OrderedDict class available from the collections module which may be of interest for you: from collections import OrderedDict
Documentation-
https://docs.python.org/2/library/collections.html#collections.OrderedDict

1 Like

The exercise is very specific and since you have an empty print to make it look nicer if you take that out it will work without a hitch.