Whats it This Time? 7/14


#1

Again. Code for the file:

animals = ["aardvark",  "badger", "duck", "emu", "fennec fox"]
duck_index = index("duck")


# Don't know what to put here




print animals

But when I press Save and Submit, an error comes from both Codacademy and the console.
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: name 'index' is not defined

And the console error:

Traceback (most recent call last):
   File "python", line 2, in <module>
NameError: name 'index' is not defined

Fix this. Please.


#2

The way you can use index:

duck_index = (variable).index("___")

Now here:

In the directions, after the first directions:

Then .insert(index, item) the string "cobra" at that index.

It says to insert cobra and the index in animals! So, what do you have to do?

animals.index(duck's index, "cobra")


#3

You mean this is the code?

animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index = animals.index("duck")   # Use index() to find "duck"

Hold on, this is the part I'm mostly confused about now.

.insert(index, item)

I forgot the index for "cobra".


#4

Yes, you got:

correct. :smiley:
NOW let's go to your most confusing part.

It says to add "cobra" to animals. So, you use "item". But if you hadn't defined it, how can you use "item"? You are actually adding "item" into the animals index. This might be confusing, so I will give an example:

blam =["BARNEY", "GOLDILOCKS", "RED RIDING HOOD"]

Now, let's say you want to add the word "RAPUNZEL" to it. So, this would be your code after it:

blam.insert("RAPUNZEL")

This would change the array "blam" to:

blam = ["BARNEY", "GOLDILOCKS", "RED RIDING HOOD", "RAPUNZEL"]

-----------------------------------------------------------------

Now, this section will be showing you what would happen if we did it the way YOU did:
Now, let's start like usual. Here's the array:

blam =["BARNEY", "GOLDILOCKS", "RED RIDING HOOD"]

Now, you want to add "RAPUNZEL". So, you know it would be an item of the array. So, you write item when inserting.

blam.insert(item)

This will cause an error.
1) "Item" is not a function.
2) "Item" is not stated as a string

-----------------------------------------------------------------

So do you get what the problem is? The problem is that you have put "item" instead of "cobra".
Another is that you put "index" here:

When you wrote duck_index.
So, this is the best code for that:

animals.insert(duck_index, "cobra")

Hope this helped!:smiley: TOOK SO LONG. (Haha!)


#5

duck_index = animals.index("duck")

animals.insert(duck_index, "cobra")

This is correct. The mistake I made is I didn't see this:

animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index =

Either delete the one you made, or delete the one at the top of the page, and this code will work fine.