Wats wrong?


#1


suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]

first = suitcase[0:2] # The first and second items (index zero and one)
middle = suitcase[2:3] # Third and fourth items (index two and three)
last = suitcase[4:5] # The last two items (index four and five)


Replace this line with your code.


#2

When you access the first and second items it would be
EDIT: This is incorrect

first = suitcase[0:1]

Instead of

first = suitcase[0:2]

#3

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


#4

The issue should be with finding the last two (2) elements in the list. You can do this by:

last = suitcase[(len(suitcase) - 2):] # The last two items (index four and five)

Others look good.

Let me know if this resolves. :wink:


#5

Ok looks like I mislead you a bit there.

first  = suitcase[0:2]  # The first and second items (index zero and one)
middle = suitcase[2:4]                # Third and fourth items (index two and three)
last   = suitcase[4:6]                # The last two items (index four and five)

...should work because it grabs from the first index up till before the second.


#6

still showing error help?????????


#7

Well....It's best to tell us what is the error message instead of just saying there's an error.


#8

check the linkout
https://www.codecademy.com/courses/python-beginner-en-pwmb1/1/2?curriculum_id=4f89dab3d788890003000096


#9

The link only leads us to our own code. Which is why I asked if you can post your error message here :slight_smile:


#10


#11

I believe you skipped over @sgt.schultz's comment here.


#12

i tried it didnt work either and if we start frm 0 how can we write 6 in the bracket???


#13

You see, [0:2] takes the first and second items. The reason why we put 2 instead of 1 is because that last number DOESN'T include it. So if you put [0:1] you're saying get items at index 0 to 1 EXCLUDING 1. If you want to include the item at index 1, you need to put [0:2]. Same for the middle and last.

EDIT:
If we put [4:5] we are saying to get items at index 4 to 5 EXCLUDING 5. So we should put [4:6] to include it.


#14

catdogfrog cat = animals[:3] and frog = animals[6:] why ???


#15

It seems as though you skipped my contribution.

suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]

first = suitcase[0:2] # The first and second items (index zero and one)
middle = suitcase[2:4] # Third and fourth items (index two and three)
last = suitcase[(len(suitcase) - 2):] # The last two items (index four and five)

"""or"""

last   = suitcase[4:] # The last two items (index four and five)

print first
print middle
print last

Dynamically :To get the last two items on the list, all you need do, is set the starting position (index) as the length of the list minus 2; then, leave the other side of the colon ( : ) empty, telling it to go to the end of the list.

Hard coding : This is my second solution to print the index of the last two items. Simply start from the 4th and let it go to the end of the list.

Also consider the edit for 3rd and 4th items indexing in my above above.

I hope this helps! :wink:


#16

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