Why are my student dictionaries not accepted?

Question

Why are my student dictionaries not accepted?

Answer

There are a few common errors listed below:

  1. Capitalization issues will prevent you from passing. Remember that Codecademy checks for exactly what the instructions ask for, so your variable names should be all lowercase, like lloyd, but their names stored under their "name" keys should be capitalized like a normal name, like "Lloyd".
  2. Incorrect dictionary syntax will give you lots of errors! Remember that dictionaries are key-value pairs, and that each value except the last in the dictionary must have a comma after it.
  3. The "name" key’s value does not go in brackets [ ], it’s just a string.
5 Likes

Can someone please explain what happens after running the code below:

students=[lloyd,alice,tyler]
def set_up(list):
  for x in list:
    x={}
    x["homework"]=[]
    x["quizzes"]=[]
    x["tests"]=[]
    x["name"]=str(x)[0].upper()+str(x)[1:len(str(x))]
  return x
print set_up(students)

I thought it might print out the dictionaries required to pass this exercise.

here:

students=[lloyd,alice,tyler]

you have a list with undefined variables.

How do you expect this to work?

1 Like

First, fix the list: students=["lloyd","alice","tyler"]
Next: look at what you are doing here:

  for x in list:
    x={}

Each time around, the variable x is assigned one of the student names, say “lloyd”. Then, the same variable, x, is re-assigned the value: an empty dictionary. That student name is lost when you re-assign your iteration variable. You need something like:

  my_dict = {}
  for x in list:
    # do something here to make x a key in my_dict, whose value is an empty dictionary.

… maybe:

if not x in my_dict:
  my_dict[x] = {}

I’ii let you take it from there.

2 Likes

Thank you for your reply!

Makes sense. I’m really new at this so I may say and think stuff that are completely against python language. So a list must contain defined data, it cannot contain undefined variables. I should go check the exercise with lists again.

how are we now going to the variables with the right names then? you need to end up with:

lloyd = {
  "homework": [],
  "quizzes": [],
  "tests": [],
  "name": "lloyd",
}

The lists and dictionaries exercise always used strings or numbers as list items. And then we worked from there - all of the items had their type defined.

students=["lloyd","alice","tyler"] is making me feel uncomfortable because I have strings in my list.
How can I transform these strings into dictionaries? That is why I first wrote
students=[lloyd,alice,tyler] but now I understand why it won’t work. I have to rethink this…

you could do:

lloyd = {}
alice = {}
tyler = {}

students=[lloyd,alice,tyler]

Yes, that is the easy way for low number of students. I wanted to define a function that would create empty dictionary for each student.

The way the exercise works will not allow what you want, if you let go of the exercise requirements you get a lot more possibilities:

class School:
    def __init__(self, *args):
        self.create_student(*args)

    def create_student(self, *args):
        for arg in args:
            setattr(self, arg, {})
            student = getattr(self, arg)
            student['homework'] = []
            student['quizzes'] = []
            student['tests'] = []
            student['name'] = arg.capitalize()


the_school = School("alice", "lloyd", "tyler")
print(the_school.lloyd)
2 Likes

This looks way more classy, but is out of my league.
Loving the student['name'] = arg.capitalize() block. So much more elegant.

Thank you for your help, you just made me buy the Pro version, I love it here!

I did as you suggested and then tried to define the function that would complete the task. I see I was trying to transform a dictionary to a string before which is a no no:

Is there even a way to complete my function with my knowledge so far? I took it step by step from the beginning in Python 2 coding, never wrote anything else before.

yea, instead of a class use a dictionary:

def create_students(*args):
    students = {}
    for arg in args:
        students[arg] = {}
        students[arg]['homework'] = []
        students[arg]['quizzes'] = []
        students[arg]['tests'] = []
        students[arg]['name'] = arg.capitalize()
    return students

print(create_students("alice", "lloyd", "tyler"))

again, this will not work with the exercise.

1 Like

lol, apparently it can be. Nice to be proven wrong sometimes:

def create_students(*args):
    for arg in args:
      	
        globals()[arg] =  {
          'homework': [],
          'quizzes': [],
          'tests': [],
          'name': arg.capitalize(),
        }

        
create_students("alice", "lloyd", "tyler")
print(alice)

I would like to add a serious note to this code, its very very bad practice. I would much rather have the classes or dictionary solution.

1 Like

I submitted my code which wasn’t excepted - when I pressed ‘solution’ I was just missing in all but the names…item? (Sorry no idea what this is called officially).

What is ? What does it do? Why can an item thing not be left blank in a dictionary if you use a comma anyway?

1 Like

This was the square brackets - not a square, obvsly!

Edit - is it because they are lists? Not item thingies?

here:

'homework': [],

you assign an empty list to homework, a student will get multiple homework grades, so you need a data type that can hold multiple values, a list can do this

on the other hand, a student name can just be represented as a string, what would be the value of using a list for the name?

Could someone please explain to me what I am doing wrong here?
lloyd = {“name”, “homework”, “quizzes”, “tests”}
alice = {“name”, “homework”, “quizzes”, “tests”}
tyler = {“name”, “homework”, “quizzes”, “tests”}
students = [name(“Lloyd”), name(“Alice”), name(“Tyler”)]

what are you trying to do?

I’m trying to print out the integer value for each student from the dictionaries. I’m very new at this so I don’t doubt a lot of it is wrong.