Lesson Number One - Syntax Issues


#1

With this code, I was getting this message: "the name key does not have the value "Lloyd" in your lloyd dictionary". I figured out that the [] brackets around the name key definitions have to be eliminated.... but my question is WHY? I thought you had to define keys in a dictionary within the square brackets? The example on this lesson page shows the animal_sounds keys defined within [] (e.g. "cat": ["meow", "purr"] ) - so what's up?

lloyd = {
"name": ["Lloyd"],
"homework": [],
"quizzes": [],
"tests": [],
}
alice = {
"name": ["Alice"],
"homework": [],
"quizzes": [],
"tests": [],
}
tyler = {
"name": ["Tyler"],
"homework": [],
"quizzes": [],
"tests": [],
}
print lloyd["name"]


#2

I think it's because the brackets make a list.

In A Day in the Supermarket, lesson 2:

webster = {
"Aardvark" : "A star of a popular children's cartoon show.",
"Baa" : "The sound a goat makes.",
"Carpet": "Goes on the floor.",
"Dab": "A small amount."
}

The dictionary has four keys and four values, but none of them are lists, unlike in the sidebar code for this lesson.

animal_sounds = {
"cat": ["meow", "purr"],
"dog": ["woof", "bark"],
"fox": [],
}
print animal_sounds["cat"]

The values for the keys are lists, and I think that's what the brackets denote.

Please, somebody, correct me if I'm wrong.


#3

I agree with @drawmesomesunshine the "name" key has no list but only one item.

Thus no need for the square brackets


@drawmesomesunshine You are not wrong

I do hope you have been assisted @pj2299
Good day. Siku Njema . :smile:


#4

If the second part of the key eg "name": "Lloyd". In this case if the word Lloyd is there then youdont need square brackets which you have included but if there is nothing there like the key for homework then you write "homework": [ ] which is two double square brackets.


#5

look back to the previous lesson: "The Next part is key"
Use ' instead of " and keep them single-lined


#6

I found the answer!

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

lloyd['name'] = "Lloyd"
lloyd['homework'] = []
lloyd['quizzes'] = []
lloyd['tests'] = []

alice['name'] = "Alice"
alice['homework'] = []
alice['quizzes'] = []
alice['tests'] = []

tyler['name'] = "Tyler"
tyler['homework'] = []
tyler['quizzes'] = []
tyler['tests'] = []

Just create an empty list!


#7

this works also and takes far less effort.

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

alice = {
"name":'Alice',
"homework":[],
"quizzes":[],
"tests":[]
}

tyler = {
"name":'Tyler',
"homework":[],
"quizzes":[],
"tests":[]
}


#8

I tried that and I still get the syntax issue.


#9

OK, yours seems to be working and I have no idea why mine is not. See mine below. Can you please explain what is wrong with it?

lloyd = {"name": "Lloyd", "homework":[], "quizzes"[], "tests"[]}
alice = {"name": "Alice", "homework":[], "quizzes"[], "tests"[]}
tyler = {"name": "Tyler", "homework":[], "quizzes"[], "tests"[]}

The error I get is "SyntaxError: invalid syntax"


#11

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

You forgot to add : after quizzes and tests


#12

Whoops, didn't notice that. Thanks for clarifying :slight_smile: