List Slicing


#1


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

I thought I had this fixed, it seems like a viable solution, but where am I going astray, and what does codecademy want me to learn? I keep getting the error message: "Oops, try again. Your message doesn't look quite right; instead of "!egassem terces eht ma I", it should be "I am the secret message!", even though it did print the correct message. Cam someone help me understand?


I kind of expected that getting the right answer would allow me to go to the next module, but maybe there is something I am missing that I don't understand yet. I waiting with open ears and open eyes. I am ready to learn. :slight_smile: May I have a few more hints or instruction on how to properly handle this? Thank you


garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"

message = garbled[0:len(garbled):2]
print message[::-1]


#2

Your output should be,

I am the secret message!

to fix that remove this,

from your print statement, and this,

from your var.

Simply put all it takes to reverse and fix this garbled text in your code is,

message = garbled[::-2]  #the -2 is the key part

#3

Thank you so much!!! Lol. I'm so ecstatic! So, in short, doing this reverses this list because of the (-), and count for every other character because of (2), correct?


#4

list slicing can have 3 values:

[start:stop:stride]

if you have a negative stride, list slicing will start at the right hand side.

I actually disagree with bandit, you can have 0 for start value and len(garbled) for stop value, but the exercise wants you to store unraveled message in the message variable.

so you could do:

garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"

message = garbled[0:len(garbled):2]
message =  message[::-1]
print message

maybe not the most elegant solution, but its also important you understand


#5

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