7. Maintaining Order


#1

So, this worked for my friends but not for me. I must be doing something wrong.

animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
print duck_index
animals.insert(duck_index, "cobra")

print animals

ERROR

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 'duck_index' is not defined

And

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


#2

you are printing duck_index and you haven't defined it earlier, duck_index need some value, before printing


#3

Could you go into more detail, I'm having trouble understanding. (I'm Icelandic)


#4

print just print something on your console, you don't need that.

duck_index is variable that needs to hold index of duck from your animal list

duck_index = animals.index("duck")

#5

Oh wow that helped, thanks a lot bruh :smiley: Really do appreciate it.


#6

Need help

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

Your code here!

animals.insert(duck_index, "Cobra")

print animals # Observe what prints after the insert operation


#7

type "cobra" not "Cobra", use lowercase


#8

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

Your code herex

animals.insert(duck_index,"cobra")
print animals # Observe what prints after the insert operation


#9

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

duck_index = animals.index("duck") # Use index() to find "duck"

print(duck_index)

animals.insert(duck_index, "cobra") # use cobra not Cobra

print(animals[2])

print (animals) # Observe what prints after the insert operation


#10

Thanks dude, your a life saver


#11

this is correct code

animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
print animals.index("duck")
duck_index = 2
animals.insert(duck_index ,"cobra")
print animals # Observe what prints after the insert operation


#12

Quiet so! Thanks :panda_face:


#13

why you print duck_index, print animal index because you want to know whats (duck) number:
print animals.index("duck")
if you want to make duck value for duck_index:
duck_index = animals.index("duck")


#14

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

Oops, try again. Make sure to pass duck_index as the first argument in insert().
whats the problem with my code?


#15

Instead of writing:

animals.insert(2,'cobra')

write this:

animals.insert(duck_index, 'cobra')

difference:

even though 2 and duck_index have the same value, this ends up being the right answer :sweat_smile: