7. Maintaining Order


#1



https://www.codecademy.com/courses/python-beginner-en-pwmb1/1/4?curriculum_id=4f89dab3d788890003000096#


"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


#2

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


#3

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.


#4

but here:

animals.insert(3, "cobra")

you use 3, you should use duck_index variable


#5

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


#6

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