List Slicing


List Slicing

I am getting this: "Oops, try again. Your message doesn't look quite right; instead of "I am the secret message!", it should be "!egassem terces eht ma I""

I expect that "I am the secret message!" is the way the lesson wanted to me to output it and not saying "it should be "!egassem terces eht ma I"""

garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
garbled = garbled[::-1]
message = garbled[::2]
print message


Your code changes the value of garbled which is later used by codecademy to check if your code is correct.

It's always a bad idea to mutate variables defined in the base code.

garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
message = garbled[::-1]
message = message[::2]
print message


aww I see, thank you! I misread the hint (below) and made this error in thinking that it wanted us to overwrite the variable. thanks again!

It's important to remember that lists are mutable (changeable) in Python, but strings aren't; when you slice a string, you get back a new string. The original string is unchanged unless you purposely "save over" it, like this:

my_string = "Monty Python"
# => Monty Python
my_string = my_string[:-7]
# => Monty


I see :slight_smile: You're very welcome!


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