12/14 - Possible bug?


I have done what is asked but when I try to submit I get

Oops, try again. Your code looks a bit off. Double check the examples in the instructions if you need help! Your code threw the following error: 'set' object does not support item deletion

(Traceback (most recent call last):
File "python", line 10, in
TypeError: 'set' object does not support item deletion)

Line 10 is the "del 'Unicorn'" which was already there when I started. Is it a bug or am I doing something wrong? Also the hints are missing for this exercise and instead tells me to "Check out the examples in the instructions if you need help!".

My code

 # key - animal_name : value - location 
zoo_animals = {"""( 'Unicorn' : 'Cotton Candy House',
'Sloth' : 'Rainforest Exhibit',
'Bengal Tiger' : 'Jungle House',
'Atlantic Puffin' : 'Arctic Exhibit',
'Rockhopper Penguin' : 'Arctic Exhibit')"""}

 # A dictionary (or list) declaration may break across multiple lines

 # Removing the 'Unicorn' entry. (Unicorns are incredibly expensive.)

del zoo_animals['Unicorn']

 # Your code here!
zoo_animals["Rockhopper Penguin"] = 'Rainforest Exhibit'

del zoo_animals["Sloth"]
del zoo_animals["Bengal Tiger"]

print zoo_animals


You will need to remove the comment tokens.


To elaborate just a tiny bit -

A dictionary has key-value pairs.
A dictionary does support deleting values in that way.

Syntax for a dictionary literal goes like:
{key1: value1, key2: value2}

A set, which is what your error message says is what you have, can be written like this:

{value1, value2, value3}

Which is fairly similar to a dictionary, but it only has values, not key-value pairs. (Or they only have keys, depending on how you see it)

You made everything inside your "dictionary" a string. So you have
Which is a set containing a string.
Print it to see for yourself.
and/or print its type.
And take note of that it says "set" in the error message, you should be expecting it to say "dict"

A dictionary does support deleting that way, a set does not.
So it's not a bug, no.


Thank you both. I removed the comment tokens just like mtf said but those were there to begin with so I thought they wanted them there in order to spread the dictionary over multiple lines when it gets printed out ( I'm sort of new, it most likely doesn't work that way I'm guessing).


Triple quotes (''' or """) mark multi-line strings and no, that string isn't converted into code or anything similar, it's just data, a series of characters, which is stored as a single value in the set data structure which is what you created with {'this is a string'}

Curly brackets {}, just like brackets [] and parentheses () will allow you to spread things out on multiple lines, nothing extra is required. For example:

my_dict = {
    key1: value1,
    key2: value2

Another way to split something up into several lines is to put a backslash \ at the end of the line which will escape the newline, it's as if it wasn't there at all:

print 1, \

Will print 1 2, but without the backslash it would just print the 1 (no newline after and perhaps a space because of the comma) and the 2 would then be ignored because it doesn't do anything, has no side-effects, it's just there.