7. Maintaining Order



"Oops, try again. The new item should have been inserted at the index previously occupied by "duck"." is the error message that pops up when I submit my code.

I'm positive I have followed the correct instructions however it says I haven't substituted cobra. Can someone please show me where I have made a mistake, Thank you for Reviewing my code.

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

# Your code here!
animals.insert(3, "cobra")

print animals # Observe what prints after the insert operation


your duck_index contains the index of your duck, which you should use when inserting cobra into the list

Currently you use a fixed number

doing this, makes your code more flexible


I'm sorry but I still don't understand where I went wrong because in the lesson it said that I had to make the duck_index equal to duck in order to swap it out with cobra.

Thank you.


but here:

animals.insert(3, "cobra")

you use 3, you should use duck_index variable


Oh, I see what you mean now. Thank you for making that more clear. The code worked, Thank you once again.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.